Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Add colored git branch name to your shell prompt

with one comment

Here’s a little trick to get the name of your current git branch in your shell prompt. No more figuring out that you have made changes to the wrong branch once it’s too late! Your shell will not change for normal (non-git) folders. But, once you change directory into a git project, you will see the branch name added to your shell prompt like such:

Macintosh-2:git_project_foo andyuser [master] $

Simply add the following to your ~/.bash_profile or ~/.bashrc file:

function parse_git_branch_and_add_brackets {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="\h:\W \u\[\033[0;32m\]\$(parse_git_branch_and_add_brackets) \[\033[0m\]\$ "

That’s all there is to it ! Try it out !

Written by Andrew Waage

October 19th, 2009 at 3:10 pm

Posted in Git and SVN

Tagged with , , ,

One Response to 'Add colored git branch name to your shell prompt'

Subscribe to comments with RSS or TrackBack to 'Add colored git branch name to your shell prompt'.

  1. git 1.7.0.4+ already has a function defined for this called __git_ps1. But the solution below has no colour:
    The \$ caused a problem in my bash, i had to remove the \

    Code:

    case “$TERM” in
    xterm*|rxvt*)
    PS1=’\u@\h:\w$(__git_ps1 “[%s]“)$ ‘
    ;;
    *)
    ;;
    esac

    jol blazey

    22 Jan 12 at 3:21 pm

Leave a Reply