Add git branch to bash prompt
本问题已经有最佳答案,请猛点这里访问。
我正在尝试将当前的git分支添加到我的bash终端提示符。我已将以下文件复制到:https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh到我的主目录,这是.bashrc中与我的提示相关的部分:
1 2 3 4 5 6 7 | source ~/.git-prompt.sh if ["$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\\[\\033[32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00;31m\\] \\W$(__git_ps1" (%s)")\\[\\033[00m\\]\\$ ' else PS1='${debian_chroot:+($debian_chroot)}\\u@\\h:\\w\\ \\W$(__git_ps1" (%s)")$ ' fi |
但是,\\\\ W $(__ git_ps1"(%s)")返回当前文件夹和分支,并且不在存储库中时返回当前目录。
是否可以使它仅在存储库中显示当前分支,而当我不在存储库中时什么都不显示?
我发现\\\\\\\\ W正在插入当前目录,该目录未在源代码中或任何人们谈论实现的地方进行注释。我已将其修复为:
1 | PS1='${debian_chroot:+($debian_chroot)}\\[\\033[32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00;31m\\]$(__git_ps1" (%s)")\\[\\033[00m\\]\\$ ' |