Stubblog


Git branch in your bash prompt
January 18, 2010, 1:31 pm
Filed under: Uncategorized

While trying to work out how the excellent tab completion works for git, I found /etc/bash_completion.d/git which mentions the __git_ps1 command, you can use this to put the current branch in your bash prompt

here’s mine:

PS1='${debian_chroot:+($debian_chroot)}\[\ 033\[01;34m\]
\u@\h\[\ 033\[00m\]:\[\ 033\[01;33m\]\w\[\ 033\[1;32m\]
$(__git_ps1 " (%s)")\[\ 033\[00m\]$ '

(Remove the space from \ 033, WordPress wouldn’t escape it properly)

The magic bit being

$(__git_ps1 " (%s)")

There are a couple of options you can set too to get it to show you the status of the repo too, see the file above for more info.