Thursday, October 7, 2010

Show the Current Git Branch in Your Unix Prompt

I picked this tip up from Xavier Shay (who happens to be a phenomenal instructor on all things database related). It's a simple function that will save you the hassle of continually typing "git status" to see what branch you're on. I've added a slight modification to keep it quiet when not in a git repo.

Add this code to your .bash_login (or whichever config file it is that you use):

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

export PS1='\[\033[33;40m\]\@ \u@\h: \w$(parse_git_branch)>\[\033[0m\]'

Then restart your terminal and you'll see the name of your curent git branch in parentheses after the path. It even adds a * to show that you're in a "dirty" state.

This

01:27 PM damoncali@macbookpro: ~/apps/ninthyard>

becomes

01:27 PM damoncali@macbookpro: ~/apps/ninthyard(master)>

or this, if you're in a dirty state

01:27 PM damoncali@macbookpro: ~/apps/ninthyard(master *)>

Sometimes it's the little things that make your day.

0 comments:

Post a Comment