在提示中显示git分支名称在屏幕中不起作用

Oha*_*had 23 git ubuntu

我更新了我的.bashrc文件,如下所示:

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$'
Run Code Online (Sandbox Code Playgroud)

它只能找到,我可以在提示符中看到我的分支名称.但是,当我运行"屏幕"时,我明白了

"-bash:__ git_ps1:找不到命令"

这可能是什么原因?

Ser*_*usM 28

此博客文章解释说,您必须先添加该行,source /etc/bash_completion.d/git然后才能使用__git_ps1.

这是完整的例子:

source /etc/bash_completion.d/git  
export PS1='\w$(__git_ps1 "(%s)") > '
Run Code Online (Sandbox Code Playgroud)

这也可以为分支机构自动完成.

使用该格式,您的提示将类似(不着色):

~/my-repo(master) > 
Run Code Online (Sandbox Code Playgroud)

  • 在Ubuntu 13.04中,似乎你必须使用`/ etc/bash_completion.d/git-prompt`(它反过来来源`/ usr/lib/git-core/git-sh-prompt`)而不是`/ etc/bash_completion.d/git`.(您可能需要四处寻找正确的完成文件来源...) (7认同)

mst*_*ger 24

我发现修改现有提示而不是定义新提示更清晰.以下代码段将git分支名称添加到现有提示符($ PS1).您可以将以下代码段添加到〜/ .bashrc文件中:

source /etc/bash_completion.d/git (for Ubuntu 12.04 or less)
source /etc/bash_completion.d/git-prompt (for Ubuntu 13.04 and higher)
PS1=$PS1'$(__git_ps1 "(%s) ")'
Run Code Online (Sandbox Code Playgroud)

如果要使用颜色的分支名称,也可以这样做:例如,颜色绿色定义为[\ e [0; 32m].我们将它添加到git_ps1函数的内部字符串中,然后使用\ [[0m]重置颜色.需要使用转义括号表示插入了"特殊"字符.

PS1=$PS1'$(__git_ps1 "\[\e[0;32m\](%s) \[\e[0m\]")'
Run Code Online (Sandbox Code Playgroud)

许多其他颜色定义可以在这里找到


Chr*_*ial 8

问题是bash需要作为登录shell运行,以便在默认的cygwin设置中使用此函数.如果你bash在cygwin bash中运行,你会遇到同样的问题.要将屏幕设置为在登录模式下运行bash,请将此行添加到〜/ .screenrc文件中:

shell -bash
Run Code Online (Sandbox Code Playgroud)


小智 7

# Add following line to /.bashrc to show Git branch name in ssh prompt
PS1='\[\033[0;31m\]\w\[\033[0;33m\]$(__git_ps1)\[\e[0m\]$ '
Run Code Online (Sandbox Code Playgroud)

\[\033[0;31m\] 是红色的

\[\033[0;33m\] 是黄色的

\[\e[0m\] 是正常的


dar*_*sky 3

加入。source ~/.bash_profile.bashrc

遇到了同样的问题,它对我有用。