问题
After searching the net, I found that one can display the git branch name with the PS1 with below
PS1="$(__git_ps1 ) "
While this is working fine, and it display the branch name in PS1 prompt. But it is displayed where-ever I navigate, even outside the git-checked-out-repo.
I would like to display the branch name only if I am under a git tracked folder/repo. Any way to achieve this? Searching SO and net didn't pop-up helpful links in this respect.
回答1:
You need to defer execution of $(__git_ps1)
until PS1
is actually displayed. Use single quotes:
PS1='$(__git_ps1)'
来源:https://stackoverflow.com/questions/31970203/linux-ps1-display-branch-name-only-inside-a-git-repo