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.

14 Answers 14


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!

p.s.: If this doesn't work off the bat, you may need to run chmod u+x ~/.git-completion.bash to grant yourself the necessary permission:

  • chmod is the command that modifies file permissions
  • u means the user that owns the file, by default its creator, i.e. you
  • + means set/activate/add a permission
  • x means execute permission, i.e. the ability to run the script
| improve this answer | |
  • 1
    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
  • 7
    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
  • 1
    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
  • 2
    (you don't have brew git but Apple default git) brew install git and restart terminal for those who are facing weird error like git checunknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] – Sachin Oct 9 '18 at 10: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 (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

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

| improve this answer | |
  • 28
    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
  • 1
    Despite what others have said about this answer, it is the one that made git autocomplete work in terminal. – Iain Aug 31 '19 at 21:12

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

That's all. This worked for me.

| improve this answer | |
  • 1
    (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 '18 at 11:18
  • 2
    On a virgin Mac OS High Sierra, the ~/.bash_profile file doesn't exist. That's fine just create the file and place that if script in there and save. Reload your shell after and it will work. – agm1984 Oct 19 '18 at 19:32
  • This worked for me. Instead of vi (That I always struggle with) just do open -e ~/.bash_profile – Imran Feb 5 at 18:51
  • For the 2nd step you will need this link – Justice Bringer Oct 12 at 7:33

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.

| improve this answer | |
  • Pretty sure I've command line tools installed, but that file doesn't exist in my case. Happy to go with brew though (and since my git is with brew also, perhaps wiser too). – drevicko Jul 25 '19 at 9:33
  • 1
    Thanks! IMO, this should be the answer with the green check mark. – geekisthenewcool Aug 30 '19 at 20:50
  • 1
    This is the most straightforward solution and it works for me – Forever Oct 11 '19 at 19:51
  • This worked for me - although I used a slightly different format. if [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then . /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash > /dev/null fi – pamcevoy Jan 29 at 14:51
  • for me the answer worked but after adding the given line to ~/.bash_profile do not forget to also run source ~/.bash_profile (especially if you create the ~/.bash_profile file for the first time) – caramba Oct 23 at 14:55

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.

| improve this answer | |
  • 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 Sep 21 '18 at 20:45
  • 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 Sep 21 '18 at 20:52

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

| improve this answer | |

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.

| improve this answer | |
  • I was getting the same issue, but I had to run brew upgrade git and brew link git (with override enabled) – Tope Apr 10 '18 at 20:13
  • 1
    Thanks for the catch @Tope. I have changed update to upgrade. – wisbucky Apr 12 '18 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.

| improve this answer | |

Git autocompletion on Catalina and newer (using Homebrew)

macOs changed from bash to zsh as default shell starting with Catalina.
So you will need specific autocomplete scripts for zsh.

Here is how to install them with Homebrew:

brew install zsh-completion

Brew's zsh-completion does in fact include git-completion.

To activate autocompletion you then need to add a small block of code to your ~/.zshrc. You may have to create that file in your user folder if it does not already exist. Note: The output of the above installation script will tell you exactly what code to add, so read that carefully and copy from there in case it has been updated since my writing here.

if type brew &>/dev/null; then
  FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

  autoload -Uz compinit

When installation has finished, read the output of the installation script.
It will instruct you not only to add code to .zshrc but also give you advice on additional actions that might be necessary, like force rebuilding zcompdump. But most likely you will not need the additional steps.

| improve this answer | |

Tried it and it works well for branches but when you do git st + tab then it shows the error like this

unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config usage: git [--version] [--help] [-C ] [-c =] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p | --paginate | --no-pager] [--no-replace-objects] [--bare] [--git-dir=] [--work-tree=] [--namespace=] []

Searched the internet and found that --list-cmds in .git-completion.bash was in latest version of the git and on my mac the git version was 2.17.2

Here are the solutions

Solution 1

  1. Check the version of the git by using git --version command
  2. Then put the same version number in the url as I did

    curl https://raw.githubusercontent.com/git/git/v2.17.2/contrib/completion/git-completion.bash -o ~/.git-completion.bash
  3. Then follow the same steps as in the top answer, put the below code in ~/.bash_profile

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

Solution 2

You should try to update the git version to the latest then it should work.

| improve this answer | |

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

| improve this answer | |
  • I have done that, precisely in the hope of not having to deal with it -- still no branch suggestions after hitting tab – Jytug Dec 4 '18 at 12:15
  • You actually do not need oh-my-zsh, or even any separate plugin system at all! Git completion comes out of the box, you just have to enable it. – SilverWolf - Reinstate Monica Apr 30 '19 at 13:58

Most users will have git installed as part of xcode command line tools at /usr/bin/git and bash completion rules for git will be available at /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash.

In this case no need to install git from Homebrew or source git completion rules from raw.githubusercontent.com. Just create a symlink as follows:

cd /usr/local/etc/bash_completion.d/
ln -s /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash git

Note: make sure you've installed and configured bash-completion package from Homebrew.

| improve this answer | |

For macOS Catalina+, in zsh:

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

Then update your ~/.zshrc with:

if [ -f ~/.git-completion.zsh ]; then
    . ~/.git-completion.zsh
| improve this answer | |

Locate your .bashrc file.

Open it and add this line at the bottom:

source /etc/bash_completion.d/git

| improve this answer | |
  • 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .