Show git branch name in bash command prompt

I work with git command in terminal a lot and I often forget what branch I was working on. Running command git branch often is not very convenient I needed to put git branch in command prompt when I was inside git repository.

I wanted my console to look like this:

eanlr@vps some-repository (master) $ 

To get that you need to add following lines to your .bash_profile file in your home directory:

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^*/d' -e 's/* (.*)/ (\1)/'
}

export PS1="\u@\h \W[\033[32m]$(parse_git_branch)[\033[00m] $ "

You can of course customize command prompt by changing PS1 variable.

2 thoughts on “Show git branch name in bash command prompt

  1. Fantastic post but I was wanting to know if you could write a litte more
    on this subject? I’d be very thankful if you could
    elaborate a little bit further. Many thanks!

  2. Howdy! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My site looks weird when browsing from my iphone. I’m trying to find a theme or plugin that might be able to resolve this problem. If you have any suggestions, please share. Many thanks!

Leave a Reply

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