Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 195 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
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 '15 at 21:15
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 '15 at 4:06
Works, but only before restart computer. After restart of my computer I should do these action one more time. – yAnTar Nov 23 '15 at 15:25
Thanks for the answer. Git's auto completion is standard on linux and always hated the fact it's not working on MacOs out of the box. But you should source the .bash file not execute it. – bjunix Jan 28 at 10:14
See also for a different tip – Michael Durrant May 5 at 22:59

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

Here's what's confusing some of you: you have brew or ports installed, but you are using an the system git in /usr/bin/. To check this:

which git

If you see /usr/bin/git then either brew isn't correctly configured, or you've not installed git via brew.

If you install git via brew, then enabling bash completions for brew will also enable bash completions for git. This is because /usr/local/etc/bash_completion actually sources everything in /usr/local/bash_completion.d, and the brew package for git installs a git completion script there.

share|improve this answer

Run this on terminal:

curl -o ~/.git-completion.bash

Then, do this:

vi ~/.bash_profile

Add this lines:

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

That's all. This worked for me.

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 '15 at 19:16
Try creating it. – crmpicco Jun 25 '15 at 8:04
Not bash_completion, git_completion which does different stuff. – Michael Durrant Feb 19 at 22:25
@MichaelDurrant It works. Trust Me. ;)… – crmpicco Feb 22 at 10:30
@crmpicco Could you please provide instructions for creating /etc/bash_completion.d/git? It would make your answer more valuable. – Dannid Mar 16 at 17:49

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.