Git autocomplete in Mac OS X

I always liked that my Git installations on Unix and Linux comes with the contrib/complete/git-completion.bash script activated or easily activated. But its harder if you use the pre-built binaries for Mac OS X.

To get autocompletion to work on Mac OS X when installing the pre-built binaries do this.
UPDATE: Instead of step 1 and 2 below you can get the completion script directly from Github. Thanks to Fredric.

curl http://github.com/git/git/raw/master/contrib/completion/git-completion.bash -O

1. Download the Git source from git-scm.com.
2. Unpack and copy the completion script git-completion.bash found in contrib/completion to your home directory.

3. Add the following to your ~/.profile or ~/.bash_profile

source ~/git-completion.bash
 
alias gco='git co'
alias gci='git ci'
alias grb='git rb'

4. Add the following to your ~/.gitconfig

[alias]
co = checkout
ci = commit
rb = rebase

Restart Terminal and start Tabbin’!

More Git tips that I found usefull can be found on Tim Dysinger’s blog http://dysinger.net/2007/12/30/installing-git-on-mac-os-x-105-leopard

8 Responses to “Git autocomplete in Mac OS X”

  • Frederic says:

    Steps 1. and 2. can simply be replaced with a single download from github:
    curl http://github.com/git/git/raw/master/contrib/completion/git-completion.bash -O

    By the way, with the git-completion.bash script comes a handy function to use in your PS1 environment variable to see the current branch of your git repository.
    For instance, putting the following in your .bash_profile:
    PS1=’\u@\h:\W$(__git_ps1 ” (%s)”)\$ ‘
    will give you something like “user@host:git-repository (master)$ ” when cd-ing into the “git-repository”

  • henke says:

    Thanks for the tip Fredric. I actually thought about it before as it would make the instructions easier. I just wanted to be sure that people downloaded the latest code but I guess the latest code is on Github. Adding it to the instruction.

  • vodkhang says:

    This tutorial is extremely useful for me. Thanks for that

    But I have a question. In Linux or Windows git environment. When I typed git pull origin m, it can auto complete to become git pull origin master for me. Can I do that in MAC as well

  • henke says:

    @vodkhang

    Nice that you liked it. :) We always try to post stuff we think can be useful for others.

    Regarding your problem I already got it working using the steps above. I can even auto complete both origin and master by writing “git pull o m

    Im using git version 1.6.6. Probably should upgrade soon. :)

    //Henke

  • Matt Huggins says:

    Worked like a charm, thanks for the awesome tip! :)

  • Eric says:

    This helps me “git” around much faster, thank you!

  • henke says:

    @eric

    Glad you liked it!

  • Really useful. Thanks.

    Just a note for beginners, in case you need it:

    After downloaded the file, I put a dot before its name, so it becomes hidden and don’t show up on Finder, which is kind of annoying.

    $ mv git-completion.bash .git-completion.bash

    Then set the path accordingly in your .bash_profile.

Leave a Reply