Last Updated: January 27, 2020
·
1.569K
· hodgesmr

Add Git branch information to bash prompt

Add the follow to your ~/.bash_profile to display the current directory's Git branch in the bash prompt:

export PS1="\[\033[0;37m\] \w \[\033[31m\]\`ruby -e \"print (%x{git branch 2> /dev/null}.each_line.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`\[\033[37m\]$\[\033[00m\] "

3 Responses
Add your response

If' you are using linux you can simply simply add the __git_ps1 variable.

GIT_PS1_SHOWDIRTYSTATE=true

GIT_PS1_SHOWUNTRACKEDFILES=true

GIT_PS1_SHOWUPSTREAM="verbose"

export PS1="\t]\[\e[${ps_color}m\]\h:\w\[\e[0m\] \$(__git_ps1 '(%s)')>

Will produce something like this:

12:55:52]machineName:/home/user/dir (branchname *% u=)>

Where * means modified files exist -- from SHOWDIRTYSTATE

% means untracked files -- from SHOWUNTRACKEDFILES

u=/+/- means same/ahead/behind remote -- from SHOWUPSTREAM

over 1 year ago ·

There's also scm_breeze, which adds awesome shortcut commands, pretty output and some keyboard shortcuts. Currently supports {bash, zsh} and {git}.

https://github.com/ndbroadbent/scm_breeze

over 1 year ago ·

+1 for what brombomb said. Only newer git versions require to 'source' the git-prompt.sh shipped with the git distribution (or source your own).

over 1 year ago ·