Thursday, January 28, 2010

Show Name of Git Branch in Prompt

The following shows you how to have a nifty prompt in OS X like
[myhostname dirname (git branch name)]$
when you are in a git project, and
[myhostname dirname]$
when you aren't. Works for me in OS X 10.6.2 (Snow Leopard) and for another co-worker that tried it in OS X 10.5.x.

Basically you just need to call the bash script that does git completion, and then set the prompt.

Updated Aug 18, 2010 with instructions for Homebrew.

Using Homebrew

This assumes you installed Git via Homebrew. If you haven't installed Homebrew or Git yet, the instructions are on the Homebrew site, which I'd recommend, but here are the commands to do that, as of August 2010:
ruby -e "$(curl -fsS http://gist.github.com/raw/323731/install_homebrew.rb)"
brew install wget
brew install git
brew update
then add this to the bottom of your ~/.bash_profile, ~/.profile, or ~/.zshrc:
# see: http://stufftohelpyouout.blogspot.com/2010/01/show-name-of-git-branch-in-prompt.html
# see also: http://superuser.com/questions/31744/how-to-get-git-completion-bash-to-work-on-mac-os-x
# see also: http://stackoverflow.com/questions/347901/what-are-your-favorite-git-features-or-tricks
# This assumes you have installed Homebrew ( http://github.com/mxcl/homebrew )
# and then installed Git via Homebrew with the default installation location:
# ruby -e "$(curl -fsS http://gist.github.com/raw/323731/install_homebrew.rb)"
# brew install wget
# brew install git
# brew update
if [ -f /usr/local/etc/bash_completion.d/git-completion.bash ]; then
  . /usr/local/etc/bash_completion.d/git-completion.bash;
  PS1='[\h \W$(__git_ps1 " (%s)")]\$ ';
fi

Using MacPorts

For this I had installed Git via the Google Code git-osx-installer.

First install MacPorts. Then do:

sudo port selfupdate
sudo port install git-core +bash_completion
then add this to the bottom of your ~/.bash_profile, ~/.profile, or ~/.zshrc:
# see: http://stufftohelpyouout.blogspot.com/2010/01/show-name-of-git-branch-in-prompt.html
# see also: http://superuser.com/questions/31744/how-to-get-git-completion-bash-to-work-on-mac-os-x
# see also: http://stackoverflow.com/questions/347901/what-are-your-favorite-git-features-or-tricks
# install http://macports.org/
# sudo port selfupdate
# sudo port install git-core +bash_completion
if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
    PS1='[\h \W$(__git_ps1 " (%s)")]\$ '
fi

Then completely close Terminal.app and reopen it. When you cd into a dir containing a git project, you will see the currently checked out git branch within parentheses at the end of the prompt.

This solution is based on this prompt tip from Casey and the setup and related bit needed in .bash_profile is based on this tip on setting up git-completion from Adam Johnson.

Update: 2014-09-30

Due to changes in git and as personal preference, I now use git from git-scm.com along with gitflow and git-flow-completion, so after those are installed, I'd add the following for my bash prompt:

source /usr/local/git/contrib/completion/git-completion.bash
# if fails, do find /usr/local/Cellar -name git-flow-completion.bash to find path and set it here
source /usr/local/Cellar/git-flow-avh/1.8.0/etc/bash_completion.d/git-flow-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
PS1='\h:\W$(__git_ps1 " (%s)") \u\$ ';

1 comment:

Turadg said...

Very handy. The PS1 is a little different on Lion. I don't remember what the default PS1 was before, but on Lion it's:


PS1='\h:\W \u\$ '

So the new would be:

PS1='\h:\W \u$(__git_ps1 " (%s)")\$ ';