简体   繁体   中英

linux PS1 - display branch name only inside a git repo

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.

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

PS1='$(__git_ps1)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM