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

6 comments:

  1. Thank you for post. Works pretty nice on my Fedora 16

    ReplyDelete
  2. Awesome, I've been looking for this for a long time. Thanks.

    ReplyDelete
  3. Thank u very very much! I've looked for this for a long time too! Now I have my favorite autocompletion for git!
    But I have a small question. Now in Terminal I have more Linux style "$
    [~]" instead of more convenient for me "Username:folder user$ ". Is it possible to change it back? :)
    Thanks in advance!

    ReplyDelete
  4. comment string "export PS1='[\w$(__git_ps1 " (%s)")]\n\$ '" in .bash_profile

    ReplyDelete
  5. Here's what I added to my .bash_profile

    source /usr/local/bin/git-completion.bash
    source /usr/local/bin/git-prompt.sh
    GITPS1SHOWDIRTYSTATE=true

    export PS1='\w$(__git_ps1 "\[\e[0;32m\]:%s:\[\e[0m\]") > '

    ReplyDelete
  6. hi...Im student from Informatics engineering nice article,
    thanks for sharing :)

    ReplyDelete