有人能解释一下为什么分支名称没有显示在我的 (bash
) 提示符上吗?
我使用的是ubuntu 16.10。我尝试使用此 site 中的说明.
# 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\] $ "
我的.bashrc
文件在这里:https://github.com/JeremieGauthier/.bashrc/blob/master/.bashrc
我也尝试了以下代码,但它也不起作用。
function color_my_prompt {
local __user_and_host="\[\033[01;32m\]\u@\h"
local __cur_location="\[\033[01;34m\]\w"
local __git_branch_color="\[\033[31m\]"
#local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
local __prompt_tail="\[\033[35m\]$"
local __last_color="\[\033[00m\]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt
最佳答案
安装时 git
附带 git-prompt.sh bash 函数 __git_ps1 来更新提示。
功能很简单,当你在 git repo 目录中 cd 时添加当前分支。
只需添加对此函数或 ps1
中可用的另一个函数的调用,并确保您的 .bashrc
或 .bash_profile
加载bash_completion
目录。
如果您的 git 版本不包含 git_prompt.sh
脚本,请手动下载它并按照它提供的说明进行操作。
关于bash - 提示中的 Git 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968420/