Get the current Git branch in your command prompt
It seems that everyone and their dog has their own way to represent the current Git branch in the command prompt. Here's mine. Stick it in your ~/.profile
.
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\]$(git branch &>/dev/null; if [ $? -eq 0 ]; then echo "\[\033[01;33m\]($(git branch | grep ^*|sed s/\*\ //))\[\033[00m\]"; fi)$ '
craig@shiny ~/sandbox/addressbook(master)$
Now with 50% cleaner code
Not long after I posted this snippet I found a post about enabling git auto-completion. If you use the auto-complete file that's bundled with Git you can use the following code to get the same prompt (and get some nifty tab-based goodies too).
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 "\[\033[01;33m\](%s)\[\033[00m\]")$ '
You can verify that I've written this post by following the verification instructions:
curl -LO http://barkingiguana.com/2008/11/15/get-the-current-git-branch-in-your-command-prompt.html.orig
curl -LO http://barkingiguana.com/2008/11/15/get-the-current-git-branch-in-your-command-prompt.html.orig.asc
gpg --verify get-the-current-git-branch-in-your-command-prompt.html.orig{.asc,}
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.