-->

How to display git branch in PS1 with other output

2019-08-07 02:49发布

问题:

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 \.



标签: git bash ps1