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

10 Answers 10

up vote 381 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
fi

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.)

  • 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
  • 1
    See also unix.stackexchange.com/q/48862/10043 for a different tip – Michael Durrant May 5 '16 at 22:59
  • 3
    If the accepted answer isn't working for anyone then try this: source ~/.bash_profile after pasting the code. Restarting the terminal will also do the same thing. – Ben Feb 2 '17 at 17:56
  • Thank you! I tried some other stuff but nothing worked (including this github.com/bobthecow/git-flow-completion/wiki/…). Your solution worked like a charm ;) – rgoliveira Jul 19 '17 at 19: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 (some would say it's the de-facto Mac OS 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.d/git-completion.bash ]; then
  . `brew --prefix`/etc/bash_completion.d/git-completion.bash
fi

(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.

  • 22
    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
  • working like a charm, many thanks. Could you please explain what is the use of script that you have got me added in ~/.bash_profile? – Vikas Bansal Jul 18 '17 at 11:47

Run this on terminal:

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

Then, do this:

vi ~/.bash_profile

Add this lines:

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

That's all. This worked for me.

  • Simple and straightforward. Thanks :) – Deep Oct 24 '17 at 5:46
  • (2nd step) If anyone prefers to open the bash_profile with a text editor, just do this: open -e .bash_profile – Nicolas Bodin-Ripert Aug 28 at 11:18

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.

  • This makes sense, but how would you "enable bash completions for brew"? You're missing how to do that, this should be part of your answer – skplunkerin 2 days ago
  • I just did this (it works, thank you) via simplified.guide/macos/bash-completion, but the auto completion is a couple seconds delayed... odd – skplunkerin 2 days ago

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.

http://denis.tumblr.com/post/71390665/adding-bash-completion-for-git-on-mac-os-x-snow-leopard

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).

If your git completion is partially broken, even after sourcing https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash, you may just need to update your git version.

For example, when I typed git check + TAB, it would correctly autocomplete git checkout. But when I typed git checkout + TAB, it did not display the list of branches, only HEAD.

The fix for me was to update my git version. I was on 2.7.0, which apparently was not compatible with the latest git-completion.bash. I ran brew upgrade git, which updated it to 2.13.0, and now it properly autocompletes my branch names.

  • I was getting the same issue, but I had to run brew upgrade git and brew link git (with override enabled) – Tope Apr 10 at 20:13
  • 1
    Thanks for the catch @Tope. I have changed update to upgrade. – wisbucky Apr 12 at 0:42

By far, the best reference I've found to solve this very issue is on SpinDance, by Connor Livingston:

If the git-completion.bash script doesn’t exist on your machine, please retrieve it from the link I provided above (which is this) and save it to your local machine in a new file called git-completion.bash in the /usr/local/etc/bash_completion.d/ directory. If the git-completion.bash script exists on your machine, but is not in the /usr/local/etc/bash_completion.d/ directory, we should create a copy of it in that directory. A quick sudo cp /current/path/to/your/git-completion.bash /usr/local/etc/bash_completion.d/git-completion.bash should do the trick. For those who are curious about the /usr/local/etc/bash_completion.d/ directory: it’s for storing new completion commands, and you may have to create it if it doesn’t already exist on your machine.

At this point the git-completion.bash script should exist on your local machine in the /usr/local/etc/bash_completion.d/ directory. Now we’ll plug the git completion script into bash by pointing to it from ~/.bash_profile.

Note: the tilde in the previous sentence refers to the home directory on your computer. Add the following line to ~/.bash_profile: source /usr/local/etc/bash_completion.d/git-completion.bash and save. The final step is to reload your bash profile. You can achieve this by running source ~/.bash_profile in your current bash session.

git and git-completion.bash already come with macOS command line tools (xcode-select --install). To enable bash autocomplete, add this to your ~/.bash_profile.

[ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ] && . /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash

This will also enable autocomplete for git branches.

New contributor
Harsh Vakharia is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Install oh-my-zsh and set zsh as your default shell and thank me later :)

Locate your .bashrc file.

Open it and add this line at the bottom:

source /etc/bash_completion.d/git

  • 1
    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 '16 at 22:25
  • @MichaelDurrant It works. Trust Me. ;) prowrestlingtees.com/media/catalog/product/cache/1/image/… – crmpicco Feb 22 '16 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 '16 at 17:49

protected by Mark Sep 20 at 12:01

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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