如何在 Bash 提示中显示带有颜色的 git 分支?

u12*_*123 192 command-line bash git

在 git 存储库中工作时,我正在使用本指南在 gnome 终端(Ubuntu 15.10)中显示分支名称。基于以上内容,我现在在 ~/.bashrc 文件中有以下内容:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes 
Run Code Online (Sandbox Code Playgroud)

...

# Add git branch if its present to PS1
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)

结果我现在得到:

在此处输入图片说明

所以它的工作原理。但是为什么我的 user@host 的颜色被删除了?而且我还希望分支名称应该是彩色的。在它看起来像这样之前:

在此处输入图片说明

更新:我现在已经尝试过本指南:

https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

将此添加到 .bashrc:

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Run Code Online (Sandbox Code Playgroud)

这有效:

在此处输入图片说明

注意在 .bashrc 中我也有这个(默认):

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
Run Code Online (Sandbox Code Playgroud)

我还没有找到为什么那个片段给出了正确的结果而另一个版本没有。对此有任何意见吗?

这是我的 .bashrc 的版本,它启用了不起作用的旧片段:

http://pastebin.com/M8kjEiH3

kos*_*kos 185

这个片段:

# Add git branch if its present to PS1

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

旨在替换默认提示定义:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Run Code Online (Sandbox Code Playgroud)

以:

unset color_prompt force_color_prompt
Run Code Online (Sandbox Code Playgroud)

.bashrc您发布显示你将它添加默认的提示定义和unset color_prompt force_color_prompt(线#64)。

要么代码片段替换默认提示定义,要么保持~/.bashrc原样,并unset color_prompt force_color_prompt在第 64 行注释默认提示定义:


所以你的 .bashrc 的一部分可能看起来像

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
# THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
#if [ "$color_prompt" = yes ]; then
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
#fi
#unset color_prompt force_color_prompt
Run Code Online (Sandbox Code Playgroud)

屏幕截图

  • 要根据其状态为分支着色,您可以使用 git 本身提供的原生 [git-prompt 脚本](http://digitalfortress.tech/tutorial/setting-up-git-prompt-step-by-step/) . (3认同)

小智 147

Ubuntu:在终端上显示您的分支名称

在 ~/.bashrc 文件中添加这些行

# Show git branch name
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)

使用以下命令重新加载 .bashrc 文件:

$ source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

  • 这在 18.04 对我有用! (13认同)
  • 在 Ubuntu 20.10 上正常工作;) (4认同)
  • 这有效并保持了颜色!(Ubuntu 18.04) (3认同)
  • 谢谢。使用:Ubuntu 18.04.1 LTS (2认同)

小智 21

现在,我按照这个 https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f工作,到目前为止喜欢它,尽管我计划进一步定制它。

在终端

mkdir ~/.bash
Run Code Online (Sandbox Code Playgroud)

将原始git-prompt.sh文件从 git contrib复制到~/.bash 目录:https : //github.com/git/git/blob/master/contrib/completion/git-prompt.sh

~/.bashrc~/.bash_profile(选择您通常放置任何 bash 自定义/设置的文件)中,添加以下行:

source ~/.bash/git-prompt.sh # Show git branch name at command prompt
export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

# Terminal Prompt:
# Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-prompt.sh)
Run Code Online (Sandbox Code Playgroud)

只要您在 git repo 中,您的 Bash 提示现在应该以颜色显示当前的 git 分支,表示它是否有未提交的更改。

  • 这应该是公认的答案,因为它清晰、简洁并且可以完成工作,并且它也适用于其他平台。 (3认同)

小智 7

将以下行附加到~/.bashrc

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true

export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
Run Code Online (Sandbox Code Playgroud)


小智 7

快速破解:

  1. 将此添加到~/.bashrc
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "
Run Code Online (Sandbox Code Playgroud)
  1. 重启终端,或者 source ~/.bashrc

在此处输入图片说明

更多细节:https : //medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745