linux PS1 - display branch name only inside a git

2019-07-24 04:19发布

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.

标签: git bash ps1
1条回答
对你真心纯属浪费
2楼-- · 2019-07-24 05:20

You need to defer execution of $(__git_ps1) until PS1 is actually displayed. Use single quotes:

PS1='$(__git_ps1)'
查看更多
登录 后发表回答