在Bash命令提示符下添加git branch

Geo*_*nos 159 git bash ubuntu

我尝试在bash提示符下添加我正在处理(签出)的git分支但没有成功..(同时保持我当前的路径显示活动目录/文件完好无损)我家里有一个.bashrc文件,但我也看到很多人提到.profile文件..

Von*_*onC 154

请注意,此类提示现在由contrib/completion /git-prompt.sh及其master变量管理.

  • 将此文件复制到某处(例如master/contrib/completion/git-prompt.sh).
  • 将以下行添加到您的__git_ps1_branch_name:
  source ~/.git-prompt.sh
Run Code Online (Sandbox Code Playgroud)
  • 将您~/.git-prompt.sh的调用更改.bashrc/.zshrc为命令替换:
  Bash: 
  PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  ZSH:  
  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
Run Code Online (Sandbox Code Playgroud)

但请注意,只有git 1.9.3(2014年5月)或更高版本允许您安全地显示该分支名称(!)

犯8976500理查德·汉森(PS1):

bash和zsh都将PS1的值置于参数扩展,命令替换和算术扩展中.

不是在__git_ps1以两个或三个参数模式运行时包含原始的,未转义的分支名称,而是构造richardhansen引用包含分支名称的变量.

因为shell不会递归扩展,所以这可以避免通过特制的分支名称执行任意代码,例如

'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.
Run Code Online (Sandbox Code Playgroud)

什么狡猾的头脑会这样命名一个分支?;)(除了在xkcd中的妈妈)


评论中的still_dreaming_1报告:

如果你想要PS1(我的PS1)颜色提示,这似乎很有效:

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)\$ ' 
Run Code Online (Sandbox Code Playgroud)

一切都是不同的颜色,包括分支.

在Linux Mint 17.3 Cinnamon 64位:

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

  • 我在Linux Mint 17.3 Cinnamon 64位中发布的PS1不再正确显示,但这样做:`PS1 ='$ {debian_chroot:+($ debian_chroot)}\[\ 033 [01; 32m \]\u @\h\[\ 033 [01; 34m \]\w\[\ 033 [00m \] $(__ git_ps1)\ $'` (5认同)
  • 当你实际上在一个git仓库中时,如何设置当前git分支的提示.因此,如果您在正常目录中,则显示正常提示. (3认同)
  • 如果您想要使用 xterm 的颜色提示(在 .bashrc 中),这似乎效果很好: `PS1='\[\e]0;\u@\h: \w\a\]\n${debian_chroot :+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\ ]$(__git_ps1)\$'` 一切都是不同的颜色,包括分支。 (3认同)
  • 当前的 master 位于 https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh - 我认为我们应该在答案中链接到它,以防将来发现漏洞? 另外:我不得不在我的 PS1 行中转义 $ 和 ",但这可能是由于它的复杂性。 (2认同)

par*_*ish 55

请按照以下步骤操作:(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\] $ "
Run Code Online (Sandbox Code Playgroud)

现在,启动新的终端窗口,并尝试进入任何git-repo.将显示当前分支,并显示提示.

4更多信息 - MAC/Linux

  • 对于颜色,请考虑将上面的 parse_git_branch 中的“gitbranch”更改为“git -c color.ui=alwaysbranch”。它与 git 分支用于突出显示当前分支的颜色相同。 (5认同)
  • 请参阅http://apple.stackexchange.com/questions/119711/why-doesnt-mac-os-x-source-bashrc和http://apple.stackexchange.com/questions/51036/what-is-the-差间的bash-档次和.bashrc中. (2认同)

Eri*_*oie 36

1-如果你没有bash-completion ......:sudo apt-get install bash-completion

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

if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi
Run Code Online (Sandbox Code Playgroud)

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

4-做 source .bashrc

编辑:

进一步阅读:不要重新发明轮子


jam*_*ing 20

以下是我如何配置提示以显示Git状态:

获取git-prompt脚本:

curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
Run Code Online (Sandbox Code Playgroud)

并自定义您的提示,在.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"
Run Code Online (Sandbox Code Playgroud)

如果你想了解更多,你可以在这里获得所有的dotfiles:https://github.com/jamming/dotfiles


小智 9

对于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\] $ "
Run Code Online (Sandbox Code Playgroud)


IFT*_*SIF 8

首先,在您的主目录中打开您的 Bash 配置文件。使用默认编辑器打开和编辑bash_profile的最简单方法。

例如,我使用 VS Code 使用以下命令打开它:code .bash_profile。

然后只需将以下代码粘贴到您的 Bash 中即可。

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\] $ "
Run Code Online (Sandbox Code Playgroud)

功能

parse_git_branch()

将获取分支名称,然后通过 PS1 您可以在终端中显示它。

这里,

\u = 用户名

@ = 静态文本

\h = 计算机名称

\w = 当前目录

$ = 静态文本

您可以更改或删除这些变量以进行更多自定义。


如果您第一次在终端中使用Git或配置后立即使用Git,有时可能看不到分支名称。

如果您遇到此问题,请不要担心。在这种情况下,只需创建一个示例存储库并在进行一些更改后提交即可。当commit命令执行一次时,终端会从此时开始查找git分支。


屏幕截图:终端中的 Git 分支


Ali*_*Alp 7

root:~/project#  -> root:~/project(dev)# 
Run Code Online (Sandbox Code Playgroud)

将以下代码添加到 ~/.bashrc 的末尾

force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt
Run Code Online (Sandbox Code Playgroud)


Jas*_* L. 5

我想要一个干净的解决方案,附加到现有提示而不是替换它。与其他解决方案一样,将其添加到 .bashrc 的底部

# function
parse_git_branch() {
  if [ -n "$(git rev-parse --git-dir 2> /dev/null)" ]; then
    echo ">> $(git rev-parse --abbrev-ref HEAD) >>"
  fi
}

# environment customization
export PS1="\$(parse_git_branch)\n$PS1"
Run Code Online (Sandbox Code Playgroud)

此设置会产生如下所示的提示

>> branchname >>
user@host:~/current/path$
Run Code Online (Sandbox Code Playgroud)

另外,我喜欢在提示中添加一点颜色,这样它就更突出了,就像这样

export PS1="\e[0;36m\$(parse_git_branch)\e[0m\n$PS1"
Run Code Online (Sandbox Code Playgroud)

这会导致分支名称以青色显示