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 2 comments

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 , , ,

2 Responses 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 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 \


    case “$TERM” in
    PS1=’\u@\h:\w$(__git_ps1 “[%s]“)$ ‘

    jol blazey

    22 Jan 12 at 3:21 pm

  2. Nice! This is really useful for me; thanks for taking the time to make a script addtion to bashrc instead of prattling on about how to do this or that as many others do!

    Very useful!


    5 Jul 12 at 10:21 pm

Leave a Reply