Adding the current Git branch to your terminal prompt

Tips and tricks / Jan. 29, 2015

Screen Shot 2016-01-29 at 1.52.15 PM

Martin Fitzpatrick has a great tip that helps me keep my sanity when working with Git branches. Googling will give you lots of ways of accomplishing this, but this one is simple, straight-forward, and works well for me. I won’t repeat his post, but basically you add a short bit of code to the .bash_profile file that lives in your home folder. Note that if this is your first time altering Bash, .bash_profile may not exist, so you’ll have to create it using Sublime Text or whatever text editor you like.

I like having color in Terminal, but I find a colored prompt distracts from the other colored content, which is usually for colored for more important or informational reasons. This post on OSXDaily has a good rundown of adding color to Terminal.

Here’s my .bash_profile which puts the Git branch in light gray.

# Enable colors
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad

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

# Prompt appearance
export PS1="\h:\W\[\033[37m\]\$(parse_git_branch)\[\033[00m\]$ "