[Mac] Show git branch name on terminal

1. Open ~/.bash_profile

If you don’t have this file, open your terminal and type in the following

 
touch ~/.bash_profile  #create file
open ~/.bash_profile   #open file

 

2. Add the following lines to .bash_profile:


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\] $ "


 

3. Re-open your terminal or do `source ~/.bash_profile` and you should see the following:

username@macbook-name current/directory/path (git-branch-name) $