Displaying Git Branch in the Command Line

August 13, 2008

I was hunting around looking for information about port from svn to git and getting git-svn to compile on OS X a little while back when I ran across Maddox’s explanation of branch name in command line. At the time I thought ‘Awesome!’ but I was on a mission so into del.icio.us it went to await later discover.

A couple of days ago I found the post again in my bookmarks and proceeded to spruce up my command line with the knowledge in Maddox’s post and in MacTip’s Customize Prompt on OS X article. The long and short of it is that the colors in my command prompt bother me so I cut them out of Maddox’s function and ended up with the following to lead off my ~/.bash_login.

Prompt Setup

function parse_git_branch { git branch --no-color 2> /dev/null | sed -e '/^[^]/d' -e 's/ (.*)/[\1]/' }

function gitPrompt { PS1="\h:\W\$(parse_git_branch) \u\$ " PS2='> ' PS4='+ ' } gitPrompt

I have to say I’m quite please with the new addition to my command line!