Enable Git Completion and Show Git Branch Name in Bash on Mac

install bash-completion

1
brew install git bash-completion

add bash-completion to your .bash_profile

1
2
3
if [ -f `brew --prefix`/etc/bash_completion ]; then
    . `brew --prefix`/etc/bash_completion
fi

show branch name

1
2
3
4
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\] $ "

References

  1. https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion
  2. http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac