Wednesday, October 17, 2012

How to configure Mac OS X terminal to support git completion and context based prompt?

Msysgit on Windows has several nice features:
  • git commands completion upon press <tab>
  • current git branch name in terminal command prompt
By default git on Mac OS X doesn't have these nice features. Let's configure OS X terminal to support them:
  1. Download two scripts (in $HOME/bin folder) from git contrib which support these features (https://github.com/git/git/tree/master/contrib/completion):
  2.  
    cd ~
    mkdir bin
    wget https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh
    wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
     
  3. Now we should update .bash_profile file:
  4. export PATH="$HOME/bin:$PATH"
    source ~/bin/git-completion.bash
    source ~/bin/git-prompt.sh
    
    export PS1='[\w$(__git_ps1 " (%s)")]\n\$ '
    
So, here we have:
  • git-completion.bash does all <tab> completion "magic"
  • git-prompt.sh is responsible for adding git current branch info, see __git_ps1 function call when defining PS1 variable