Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

On my linux machine I have autocomplete for branches with git.

For example I can type git checkout [TAB] and get a list of branches.

Or I can type git checkout feb* [TAB] to get all branches that begin with "feb".

How can I get this functionality on a Mac?

I tried downloading and running bash-completion/bash_completion and bash-completion/bash_completion.sh.in but no joy. The first gave error messages about declare: -A invalid_option. The second gave no errors. When I git checkout [TAB] though I am still getting a list of files in the directory, not branches. I am in the project root.

Auto-complete for directories and bash commands are working ok, so it's specific to git.

share|improve this question
Auto completion is provided by the shell (bash in this case). Can't you just copy the autocompletion settingfs from your Linux box onto your Mac? –  patrix Jul 8 '12 at 18:37
sure, what and where are those settings that I have never had to look at in Linux ? –  Michael Durrant Jul 8 '12 at 18:38
Look for any complete statements in your .bashrc (or .bash_profile) on the Linux box. As for the downloaded files, where did you download them from? –  patrix Jul 8 '12 at 18:46
Added more info, specifically: " Auto-complete for directories and bash commands are working ok, so it's specific to git. " –  Michael Durrant Jul 8 '12 at 20:45
my linux .bashrc's don;t have it. –  Michael Durrant Jul 8 '12 at 20:49

3 Answers 3

up vote 71 down vote accepted

ok, so I needed the git autocompletion script.

I got that from this url:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

No need to worry about what directory you're in when you run this as your home directory(~) is used with the target.

Then I added to my ~/.bash_profile file the following 'execute if it exists' code:

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash

Update: I'm making these bits of code more concise to shrink down my .bashrc file, in this case I now use:

test -f ~/.git-completion.bash && . $_

Note: $_ means the last argument to the previous command. so . $_ means run it - "it" being .git-completion.bash in this case

This still works on both Ubuntu and OSX and on machines without the script .git-completion.bash script.

Now git Tab (actually it's git TabTab ) works like a charm!

(PS: If this doesn't work off the bat, you may need to run chmod -X ~/.git-completion.bash to give the script permission to run.)

share|improve this answer
Any idea how to clear old branch names from the suggestion list? I've git branch -d on every local and remote branch, but I still get old branches in autocomplete. –  user Mar 4 at 5:37
^Ah, found it. stackoverflow.com/questions/13368098/… –  user Mar 4 at 5:41
Thank you, this is insanely useful. –  Feng Huo Jul 22 at 21:28

Some people prefer to use a package manager to automate the installing, updating, and configuring of MAC OS packages. I am a fan of package managers, and I think Homebrew is the best package manager for MAC OS X (some would say it's the de-facto OS-X package manager).

Once you have installed homebrew, you can simply install git along with bash-completion with brew install git bash-completion.

You'll then need to put the following in your ~/.bash_profile:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion

(Note: If this install fails with a 404 error, and you already have git installed, just remove the git part of this brew install)

more info on installing git and bash-completion with brew here: https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion

share|improve this answer
How does this differ from the accepted answer (the one with the green checkmark)? –  patrix May 29 '13 at 18:04
only that brew simplifies package management on the mac. –  Lane Jul 17 '13 at 17:26
Answers on Ask Different need to be more than just a link. It's okay to include a link, but please summarize or excerpt it in the answer. The idea is to make the answer stand alone. –  patrix Jul 17 '13 at 17:28
official homebrew installation instructions brew.sh –  Lane Aug 8 '13 at 22:38
This answer is for bash-completion and for installing git. Use the accepted answer to install git auto-completion, which the question is leaning towards. –  Danny Mar 5 at 15:25

You could install bash and git from MacPorts with the "bash_completion" option enabled. It is probably the easiest way of maintaining both git and the completions up-to-date.


To install MacPorts: http://www.macports.org/install.php/

Additional info

As for locating the completion settings on your Linux system, have a look in /etc/bash_completion.d/git (it is a long file). Copying that by itself probably won't work, though, as it is designed to be sourced by the completion framework through /etc/bash_completion (which is sourced from /etc/profile.d/bash_completion.sh; files in /etc/profile.d are sourced from /etc/profile).

share|improve this answer
macports is old and I am using homebrew. I've posted my answer above for others. – –  Michael Durrant Nov 16 '12 at 18:26
this answer is also about bash-completion NOT git-completion.bash –  Michael Durrant Jun 17 at 20:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.