linux PS1 - display branch name only inside a git repo

你离开我真会死。 提交于 2019-12-11 03:01:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!