Sign up ×
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.

[Note we are talking about git branch completion, not bash completion (e.g. commands, files, etc). Thus NOT lane's answer at all]

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/ 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

4 Answers 4

up vote 138 down vote accepted

ok, so I needed the git autocompletion script.

I got that from this url:

curl -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
Works like a charm!! :) :D thnx –  mayankcpdixit Sep 7 '14 at 7:16
The laconic version of the bash profile didn't work for me on Yosemite (full if did) so FYI. But otherwise awesome tip, thanks! –  JasonCoder Mar 12 at 21:15
I am using Yosemite and not having any problem with the one-line version –  Michael Durrant Mar 19 at 20:08
Maybe you needed to do a new terminal session perhaps or . .bashrc ? –  Michael Durrant Mar 19 at 20:09
Yep, restarting terminal after adding the bash script made it work. I was sitting here wondering why it kept trying to autocomplete to my Models folder when I hit m Tab to make it autocomplete to master. –  alex Jun 10 at 4:06

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:

share|improve this answer
Adding git and adding bash-completion is not at all the same as adding .git-completion.bash They are different programs and they do quite different things - one does bash command autocompletion and the other does git command autocompletion and 'nair the twain will meet ;) –  Michael Durrant Sep 8 '14 at 19:49

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:

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/; files in /etc/profile.d are sourced from /etc/profile).

share|improve this answer

Locate your .bashrc file.

Open it and add this line at the bottom:

source /etc/bash_completion.d/git

share|improve this answer
I don't have anything an /etc/bash_completion.d/ folder, on a relatively new system (OSX 10.10.3). –  livingtech Jun 24 at 19:16
Try creating it. –  crmpicco Jun 25 at 8:04

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.