HTML5, CSS3, jQuery, JSON, Responsive Design...

How to show current Git branch name in your terminal

Michael Brown   April 23 2016 09:36:55 PM
If you're using Git for your source control (and really, if not, why not?), then seeing what branch you're currently working on is a rather important feature!  Sadly, it's not one that's built into the terminals on either OS X or Linux.

Fortunately, you can implement this by editing you bash profile config file.  This is how the terminal looks on my Mac after I did this.  You can see that the Git branch name, "master" in this case, is shown in green:
OS X terminal showing the Git branch name

Here's the code that you need to add to your bash profile config file:
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'll find the files to edit on your home folder on both OS X on Linux.

On OSX, it's the file ~/.bash_profile.

On Linux, it's ~/.profile.  (There maybe differences here between different distributions, that worked for me on Linux Mint.)

The files will be hidden by default.  Tip: on OS X, you can hit Command->Shift->full stop (that's a period to some of you) to show hidden files in an OS X File Open dialog.






Comments

1Mark Leusink  04/23/2016 1:38:59 AM  How to show current Git branch name in your terminal

Or... Install Oh My Zsh ({ Link } ) as a far better terminal on Mac. It has plugins for a lot of things, including Git integration. I particularly like the color coding.

2Liquid prompt   04/23/2016 2:45:18 AM  How to show current Git branch name in your terminal

Liquid prompt is also quite good >> https://github.com/nojhan/liquidprompt

About