Quick and easy way to show the current Git branch on your command prompt

January 14, 2014

If you work with lots of branches in Git it is nice to be able to see at a glance which branch you are on instead of typing git branch each time. Here are 4 quick lines you can add to your bash prompt that will show the current branch.

You will need to edit your user bash config which can be in different locations depending on your operating system.

  • OS X: ~/.profile
  • Linux General: ~/.bash_profile

~ represents the path to your user home directory. For my user, blobaugh, on OS X the path is /Users/home/blobaugh.

Add in the file the following lines:

parse_git_branch() {
       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
}
export PS1="u@h W[33[31m]$(parse_git_branch)[33[00m] $ "

Restart your terminal and change directories to your repository and you should now see the branch in parenthesis at the end of your prompt.

Screen Shot 2014-01-14 at 3.55.32 PM

Leave a Reply

Your email address will not be published. Required fields are marked *