2012-03-15 52 views
21

我更新了我的.bashrc文件如下工作:顯示的Git分支名稱不以屏幕

PS1='[\[email protected]\h \W$(__git_ps1 " (%s)")]\$' 

它只是發現,我可以在提示符下看到我的分支名稱。然而,當我運行的 「屏幕」,我得到

「-bash:__git_ps1:找不到命令」

什麼可以這樣做的原因?

+0

看看這個帖子: http://stackoverflow.com/questions/2231214/git-tips-and-tricks-display-branch-on-command-prompt-not-working-and-created- s – ChristofferH 2012-03-15 09:59:33

回答

3

add source ~/.bash_profile in .bashrc

有同樣的問題,它只是爲我工作。

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

\[\033[0;31m\]是紅色

\[\033[0;33m\]是黃色

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

8

的問題是,慶典需要運行作爲登錄shell這個功能是默認提供cygwin設置。如果你在cygwin bash中運行bash,你將會遇到同樣的問題。要設置屏幕運行在登錄模式下的bash,這條線添加到您的〜/ .screenrc文件:

shell -bash 
+1

在OSX上爲我工作,謝謝! – 2013-06-04 12:21:28

27

This blog post解釋說,你必須添加行source /etc/bash_completion.d/git可以使用__git_ps1之前。

下面是完整的例子:

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

這也使得對分支機構自動完成。

使用的格式,你的提示將類似於(無着色):

~/my-repo(master) > 
+0

你怎麼做,但仍保留正常的提示信息? (即,只需追加相應的名稱) – srcspider 2013-03-22 11:49:31

+7

在Ubuntu 13.04中,似乎必須獲取'/ etc/bash_completion.d/git-prompt'(它依次從/ usr/lib/git-core/git- sh-prompt')而不是'/ etc/bash_completion.d/git'。 (您可能需要查看正確的完成文件來源...) – michael 2013-04-26 08:39:46

+1

__git_ps1已移出git-completion。有關更多信息,請參閱http://stackoverflow.com/a/12871094/551045 – RedX 2013-10-30 12:30:14

22

我覺得清潔修改,而不是定義一個新的現有提示。以下片段將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) ")' 

如果你想在顏色的分支名稱,你可以做到這一點: 例如,色綠被定義爲\ E [0;32米]。我們將這添加到git_ps1函數的內部字符串中,並使用\ e [0m之後重置顏色。轉義括號需要指出插入了「特殊」字符。

PS1=$PS1'$(__git_ps1 "\[\e[0;32m\](%s) \[\e[0m\]")' 

許多其他的顏色定義can be found here

+0

要根據其狀態(乾淨/髒/隱藏/等)對分支着色,可以使用此** [git prompt script](http://digitalfortress.tech/tutorial/setting-up-git-prompt-step逐步驟/)**。 – 2018-01-15 12:03:05

0

這是在Debian/Ubuntu的測試。


  1. 安裝bash-completion
  2. 請確保您~/.bashrc存在以下線和沒有被註釋掉。

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then 
    . /etc/bash_completion 
fi 
0

如果你沒有__git_ps1你可以使用

git branch --contains HEAD 2>/dev/null 

它顯示了同樣喜歡__git_ps1。

如果你創建這樣一個別名:

alias __git_ps1='git branch --contains HEAD 2>/dev/null' 

例如你prombt你用這個命令:

$PS1='[\[email protected]\h \W(`__git_ps1`)]\$' 

PS1='[\[email protected]\h \W\[\033[36m\](`__git_ps1`)\[\033[0m\]]\$' 

,如果你喜歡的顏色

你的腳本,使用__git_ps1你PROMT將工作完美。