在shell提示符下显示git分支?

dan*_*mcc 4 git branch prompt .bash-profile osx-lion

我试图让我的shell提示符显示当前的git分支名称.

我已经阅读了一些教程和博客文章等,据我所知,我正在做正确的事情,但它似乎没有起作用.

我想提示看起来像这样:

dannys-macbook:hillcrest-store [master]$
Run Code Online (Sandbox Code Playgroud)

但目前它看起来像这样:

dannys-macbook:hillcrest-store danny$ 
Run Code Online (Sandbox Code Playgroud)

我在〜/ .bash_profile中添加了以下内容:

PATH=$PATH:/usr/local/bin; export PATH

COLOR1="\[\e[1;32m\]"
COLOR2='\[\e[1;1m\]'
COLOR3='\[\e[m\]'
GIT_STATUS=$(__git_ps1 " %s")
PROMPT_CHAR="$"

PROMPT="${COLOR1}\u@\h${COLOR3} \w${COLOR2}${GIT_STATUS} ${COLOR2}${PROMPT_CHAR$
PS1="$PROMPT"
export PS1
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么,也许我应该以某种方式'重置'提示?

Mar*_*nas 12

更简单的解决方案:引用GIT_STATUS以便它不会在bash启动时得到评估,而是在bash显示提示时进行评估:

COLOR1='\[\e[1;32m\]'
COLOR2='\[\e[1;1m\]'
COLOR3='\[\e[m\]'
GIT_STATUS='$(__git_ps1 " %s")'
PROMPT_CHAR='\$'
PS1="${COLOR1}\u@\h${COLOR3} \w${COLOR2}${GIT_STATUS} ${COLOR2}${PROMPT_CHAR}"
Run Code Online (Sandbox Code Playgroud)

另请注意,导出PS1不是一个好主意.