Monday, February 4, 2013

Git branch in bash prompt in bold

If you are using git in your linux box, i'm sure that you wanted the current branch your working to be displayed some where in the bash shell so that you don't need to do a git branch. Well i learnt recently its possible to show the current working branch in your bash prompt. And i wanted it to be highlited in bold which was not the way it was by default. After doing some reading how to customize the bash prompt i was able to get it done.

First you need to have the bash auto completion package installed.

Then you need to add the following line to you .bachrc file to import the git prompt function.

source /usr/share/git-core/contrib/completion/

Then you need to customize the default prompt by adding the following line

export PS1='[\u@\h \W\[\e[1m\]$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")\[\e[0m\]]\$ '

Now open a new terminal and navigate to a git repo. Your prompt will look like below.