如何在 Bash 提示符中用颜色显示 git 分支?

如何在 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 

...

# 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

结果我现在得到:

在此处输入图片描述

所以作品。但是为什么我的 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\] $ "

并且有效:

在此处输入图片描述

请注意在 .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

我还没有找到为什么该代码片段给出正确结果而其他版本却没有给出正确结果的原因。对此有什么意见吗?

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

http://pastebin.com/M8kjEiH3

答案1

此代码片段:

# 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

旨在代替默认提示定义:

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

.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

屏幕截图

答案2

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

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

$ source ~/.bashrc

答案3

目前,我关注的是 https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f并且正常工作,到目前为止我很喜欢它,但我计划进一步定制它。

在终端

mkdir ~/.bash

将原始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)

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

答案4

将以下行附加到~/.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\]\$ '

相关内容