当前位置: 首页>>技术问答>>正文


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

, , ,

问题描述

我在git存储库中工作时使用this guide来显示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

结果我现在得到:

command-line,bash,git,ubuntu

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

command-line,bash,git,ubuntu

更新:我现在尝试了这个指南:

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\] $ "

这工作:

command-line,bash,git,ubuntu

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

最佳解决办法

这个片段:

# 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,并在第64行注释默认提示定义和unset color_prompt force_color_prompt


所以.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

command-line,bash,git,ubuntu

次佳解决办法

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

第三种解决办法

现在,我跟着这个https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f并且正在工作,喜欢它到目前为止,虽然我打算进一步定制它。

In Terminal

mkdir ~/.bash 

Copy the raw git-prompt.sh file from git contrib in to the ~/.bash directory: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Inside ~/.bashrc or ~/.bash_profile (choose the file where you normally put any bash customizations/setup), add the lines:

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) 

As long as you’re inside a git repo, your Bash prompt should now show the current git branch in color signifying if its got uncommitted changes.

第四种办法

转到主文件夹

单击Ctrl + h以显示隐藏文件。

打开。 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\] $ "

如果您打开终端,请关闭并再次打开。请享用!!

参考资料

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