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
UPDATE: 2011-01-03 All github.com url are now https. Thanks to Julio.
curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -O
UPDATE: 2011-07-15 Added -L option to allow for curl to handle redirects.Thanks to michaelyoung.
curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL
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

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”
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.
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
@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
Worked like a charm, thanks for the awesome tip!
This helps me “git” around much faster, thank you!
@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.
I got errors when using the autocomplete code to work on OSX Snow Leopard. I found alternative autocompletion code at: https://github.com/markgandolfo/git-bash-completion
The included readme.txt worked except it needed an extra line in my .bash_profile:
. /opt/local/etc/bash_completion/git-completion.bash
Always had problems with auto-complete on the Mac. Hopefully this will help. Thank you!
[...] Code That Matters : Git autocomplete in Mac OS X [alias] co = checkout ci = commit rb = rebase (tags: git bash completion howto) [...]
i’ve been looking for a tutorial on this! you’ve saved me great work
I couldn’t download the file using curl -O because the url was wrong, you should try this one instead:
https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
Updated url to use HTTPS instead. I guess curl does not redirect automatically.
Thanks!
[...] Git tab autocomplete for OSX – enables tab autocomplete for most git commands [...]
[...] thanks to hehejo for some bash code and to http://www.codethatmatters.com/2010/01/git-autocomplete-in-mac-os-x/ for a solution to the autocompletion [...]
Mehndi Designs…
[...]listed here are quite a few links to sites that we link to because we think they are worth visiting[...]…
Gyro Bowl…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Roth IRA Rules…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
[...] Then restart your terminal Reference: http://denis.tumblr.com/post/71390665/adding-bash-completion-for-git-on-mac-os-x-leopard http://www.codethatmatters.com/2010/01/git-autocomplete-in-mac-os-x/ [...]
I had to add the -L option. The https url has a 302 in there that redirects you to another location.
Ah thanks michael. I’ve updated the the curl command!
[...] http://www.codethatmatters.com/2010/01/git-autocomplete-in-mac-os-x/ [...]
I wrote a one-liner that’ll install this: eval “$(curl -s https://gist.github.com/raw/972430/install-git-completion.sh)”
for more info: “http://johnfgibb.com/how-to-install-git-autocompletion
Thanks John!
Would you mind updating that script to use the -L option in the curl command so that it would handle any redirects that github throws at it?
[...] or simply wanted to be more certain of a name and had to do a git banch just to see the names etc. This tool allows you to tab-autocomplete a partial branch name much the same way bash [...]
JOY! Thank you for the simple directions!
Just installed this and it works great. Thanks!
You might want to add a note though: if you add the source etc lines to your .profile file you’ll need to log in and out in order to get the changes applied. Closing the terminal will not suffice, unlike when pasting those lines into .bash_profile, as .bash_profile is reloaded when a new terminal is opened.
[...] your git branch and status in your shell prompt. RVM status, too. And it’s not hard to add autocompletion of Git branches, too. Or, go all the way to zsh, which has fantastic autocompletion. Again, reducing cognitive load [...]
Finally one that works for me! I was losing hope at this bash-completion thing, I’ve tried like 10 tutorials and none have worked. Thank you!
For me, this didn’t work with aliases.
I have aliases like this:
alias ‘pull’='git pull’
alias ‘push’='git push’
alias ‘co’='git checkout’
…
To get them to work with autocompletion, I had to also add the “complete” command to .bash_profile:
complete -o default -o nospace -F _git_push push
complete -o default -o nospace -F _git_push pull
complete -o default -o nospace -F _git_checkout co
…
I’m working on a large project, so the autocompletion feature returns hundreds of options unless I provide some prefix. Any way that I can have auto complete after the ‘git checkout’ command just return my local branches?
Hmm interesting. I’ll certainly take a look and see if I need to update the post. Thanks!
Noce tip! BTW – oh-my-zsh will give you this and much more for GIT. https://github.com/robbyrussell/oh-my-zsh .