How to display git branch in PS1 with other output?

左心房为你撑大大i 提交于 2019-12-14 02:33:48

问题


I got to display just the branch name in the PS1 if I am in a git repo linux PS1 - display branch name only inside a git repo, but I am not able to get it integrated with my earlier PS1, which is

 PS1="[\D{%F} \@] \u /\W $ "

this displays, datetime + username

 [2015-08-20 11:38 PM] mtk 

I tried

PS1="[\D{%F} \@] \u /\W $(__git_ps1) $ "

but, i get error

__git_ps1: command not found
<empty PS1>

Any way to get git branch name along with earlier details that my PS1 used to output?

EDIT: I also tried

 PS1="[\D{%F} \@] \u /\W '$(__git_ps1)' $ "

but, this gives empty quotes always, even inside a git repo.

The question is different from that one. I was to display time and date + git branch (if its inside a repo).


回答1:


PS1="[\D{%F} \@] \u /\W \$(__git_ps1) $ "

or

PS1='[\D{%F} \@] \u /\W $(__git_ps1) $ '

You must escape the $ in order to be executed each time, not only once. You can do so by using single quotes or escaping it with a \.



来源:https://stackoverflow.com/questions/32125564/how-to-display-git-branch-in-ps1-with-other-output

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