You can use the builtin __git_ps1
:
If you don't have the command __git_ps1
, then add the following line in your .bash_profile
to source git-completion.bash
:
source /path/to/git-completion.bash
You can find the file git-completion.bash
using:
mdfind git-completion.bash
Once it is done, you need to add a call to __git_ps1
in your $PS1
variable (which defines your prompt). For that, you need to redefine the variable in your .bash_profile
. Here is an example:
PS1='\h:\W \u $(__git_ps1) \$'
If you already defined it before (to customise it for example), you can add in it the part $(__git_ps1)
.
Finally, restart your terminal.
As an extra, you can set in your .bash_profile
the following:
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUPSTREAM=auto
The first one will allow to display a *
if you have local modifications and a +
if you have staged changes. The second one will allow to display a $
if you have stashes. The third one displays info about your upstream (>
if you are ahead and can push, <
if you are behind and can pull, <>
if you diverged).
It would look like that:
