当前位置: 首页>>技术教程>>正文


在Bash命令提示符下添加git分支

, ,

问题描述

我尝试在bash提示符下添加我当前正在使用的git分支(checked-out),但没有成功..(同时保持了当前路径,该路径完整显示了活动目录/文件)我的家中有一个.bashrc文件,但是我还看到许多人提到.profile文件。

最佳办法

请注意,这种提示现在由(旧的2014:contrib /completion /git-prompt.sh)最新的mastermaster/contrib/completion/git-prompt.sh及其__git_ps1_branch_name变量管理。

  • Copy this file to somewhere (e.g. ~/.git-prompt.sh).
  • Add the following line to your .bashrc/.zshrc:
  source ~/.git-prompt.sh
  • Change your PS1 to call __git_ps1 as command-substitution:
  Bash: 
  PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  ZSH:  
  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

但请注意,只有git 1.9.3(2014年5月)or later允许您安全地显示该分支名称(!)。

参见Richard Hansen的commit 8976500(richardhansen):

Both bash and zsh subject the value of PS1 to parameter expansion, command substitution, and arithmetic expansion.

Rather than include the raw, unescaped branch name in PS1 when running in two- or three-argument mode, construct PS1 to reference a variable that holds the branch name.

Because the shells do not recursively expand, this avoids arbitrary code execution by specially-crafted branch names such as

'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.

哪个狡猾的头脑会这样命名分支? ;)(Beside a Mom as in xkcd)


still_dreaming_1报告in the comments

This seems to work great if you want a color prompt with xterm (in my .bashrc):

PS1='\[\e]0;\u@\h: \w\a\]\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\03‌​3[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ ' 

Everything is a different color, including the branch.

在Linux Mint 17.3 Cinnamon 64位中:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\]$(__git_ps1) \$ ' 

次佳办法

请按照以下步骤操作:(Linux)

编辑文件~/.bashrc,在其末尾输入以下行(如果是Mac,则文件为~/.bash_profile)

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

现在,启动新的终端窗口,然后尝试输入任何git-repo。当前分支将显示,并带有提示。

4 More Info – MAC/Linux

第三种办法

1-如果您没有bash-completion …:sudo apt-get install bash-completion

2-编辑您的.bashrc文件并检查(或添加):

if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi

3- …在提示行之前:export PS1='$(__git_ps1) \w\$ '(__ git_ps1将显示您的git分支)

4-做source .bashrc

编辑:

延伸阅读:Don’t Reinvent the Wheel

第四种办法

这是我配置提示以显示Git状态的方式:

获取git-prompt脚本:

curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

并自定义提示,在.bashrc文件中添加以下代码:

# Load Git functions
source ~/.git-prompt.sh

# Syntactic sugar for ANSI escape sequences
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m'   # Black - Background
bakred='\e[41m'   # Red
badgrn='\e[42m'   # Green
bakylw='\e[43m'   # Yellow
bakblu='\e[44m'   # Blue
bakpur='\e[45m'   # Purple
bakcyn='\e[46m'   # Cyan
bakwht='\e[47m'   # White
txtrst='\e[0m'    # Text Reset

# Prompt variables
PROMPT_BEFORE="$txtcyn\u@\h $txtwht\w$txtrst"
PROMPT_AFTER="\\n\\\$ "

# Prompt command
PROMPT_COMMAND='__git_ps1 "$PROMPT_BEFORE" "$PROMPT_AFTER"'

# Git prompt features (read ~/.git-prompt.sh for reference)
export GIT_PS1_SHOWDIRTYSTATE="true"
export GIT_PS1_SHOWSTASHSTATE="true"
export GIT_PS1_SHOWUNTRACKEDFILES="true"
export GIT_PS1_SHOWUPSTREAM="auto"
export GIT_PS1_SHOWCOLORHINTS="true"

如果您想了解更多信息,可以在这里获取所有的点文件:https://github.com/jamming/dotfiles

第五种办法

对于Mac,这确实非常有效:http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/8189.html,未经允许,请勿转载。