将 git branch 添加到雪豹的 bash 提示

cra*_*ent 8 bash git osx-snow-leopard bashrc macos

我正在使用这个:

$(__git_ps1 '(%s)')
Run Code Online (Sandbox Code Playgroud)

它可以工作,但是当我更改目录或签出新分支时它不会更新。我也有这个别名:

alias reload='. ~/.bash_profile'
Run Code Online (Sandbox Code Playgroud)

示例运行:

user@machine:~/dev/rails$cd git_folder/
user@machine:~/dev/rails/git_folder$reload
user@machine:~/dev/rails/git_folder(test)$git checkout master
Switched to branch 'master'
user@machine:~/dev/rails/git_folder(test)$reload
user@machine:~/dev/rails/git_folder(master)$
Run Code Online (Sandbox Code Playgroud)

如您所见,它设置正确,但前提是我重新加载 bash_profile。我在这上面浪费了很多时间。我在雪豹上使用 bash。请帮忙!

编辑:您可以在 github 上看到我的 dotfiles 以及在此处设置 PS1 的文件

cra*_*ent 9

好的问题解决了。

我在做:

export PS1="$(__git_ps1 '(%s)')"
Run Code Online (Sandbox Code Playgroud)

我需要做:

export PS1="\$(__git_ps1 '(%s)')"
Run Code Online (Sandbox Code Playgroud)

你需要逃避美元符号,否则它只会运行一次并被替换。