485

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.

5
  • I just joined this community so I can't yet answer, but I found "oh-my-zsh" to be super super helpful and I really like the UI: github.com/ohmyzsh/ohmyzsh This is, of course, for people using zsh. I'm assuming it doesn't work for bash... Commented Jan 30, 2021 at 17:07
  • Now that the Mac uses zsh instead of bash this answer may prove useful.
    – dumbledad
    Commented Dec 1, 2021 at 15:16
  • 2
    for zsh users: echo 'autoload -Uz compinit && compinit' >> ~/.zshrc && . ~/.zshrc link
    – JillAndMe
    Commented Oct 14, 2022 at 2:58
  • 1
    1. Go to this url github.com/git/git/tree/master/contrib/completion 2. Follow the instructions in the completion file that fits your usage, whether its bash or zsh Commented Jan 9, 2023 at 13:44
  • I tired a few answers but none worked, so I tried the suggestion at macinstruct.com/tutorials/…. Same as JillAndMe's suggestion. Then it worked
    – Jimmy Long
    Commented Mar 27, 2023 at 1:38

16 Answers 16

618

Ok, so I needed the git autocompletion script.

I got that from this url using the following command in the Terminal app:

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!

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
16
  • 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
    Commented Jun 10, 2015 at 4:06
  • 1
    See also unix.stackexchange.com/q/48862/10043 for a different tip Commented May 5, 2016 at 22:59
  • 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
    Commented Jul 19, 2017 at 19:06
  • 3
    (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
    Commented Oct 9, 2018 at 10:28
  • 9
    git-completion.bash is obsolete: MacOS users should use the zsh completion wrapper for git git-completion.zsh instead: github.com/git/git/blob/master/contrib/completion/…
    – Nefeline
    Commented Oct 30, 2021 at 10:31
123

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.

5
  • 35
    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 ;) Commented Sep 8, 2014 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? Commented Jul 18, 2017 at 11:47
  • 3
    Despite what others have said about this answer, it is the one that made git autocomplete work in terminal.
    – Iain
    Commented Aug 31, 2019 at 21:12
  • Note that the lines to add into the ~/.bash_profile were specified for me in the output of homebrew. I used those instead of the lines above. Then you need to reload the bash profile in each terminal window you would like to use autocompletion in, by using the command source ~/.bash_profile
    – nyarasha
    Commented Jan 7, 2021 at 19:58
  • 1
    I did not particularly want to install Git from Homebrew; for the record, the bash-completion Brew formula provides Bash completion for much more than just Git.
    – tripleee
    Commented Dec 1, 2022 at 10:25
44

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.

5
  • 1
    (2nd step) If anyone prefers to open the bash_profile with a text editor, just do this: open -e .bash_profile Commented Aug 28, 2018 at 11:18
  • 3
    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
    Commented Oct 19, 2018 at 19:32
  • This worked for me. Instead of vi (That I always struggle with) just do open -e ~/.bash_profile
    – Imran
    Commented Feb 5, 2020 at 18:51
  • For the 2nd step you will need this link Commented Oct 12, 2020 at 7:33
  • Useful accompanying link if you have alias(es) set up for git -- gist.github.com/JuggoPop/10706934
    – sfletche
    Commented Jun 6, 2022 at 20:19
29

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.

7
  • 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
    Commented Jul 25, 2019 at 9:33
  • 4
    Thanks! IMO, this should be the answer with the green check mark. Commented Aug 30, 2019 at 20:50
  • 2
    This is the most straightforward solution and it works for me
    – F.S.
    Commented Oct 11, 2019 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
    Commented Jan 29, 2020 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
    Commented Oct 23, 2020 at 14:55
28

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

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
  compinit
fi

Important:
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.

5
  • 3
    The terminal show an annoying message each time that it's opened: zsh compinit: insecure directories, run compaudit for list. Ignore insecure directories and continue [y] or abort compinit [n]? But it works! Commented Nov 28, 2020 at 20:19
  • I just joined this community so I can't yet answer, but I found "oh-my-zsh" to be super super helpful and I really like the UI: github.com/ohmyzsh/ohmyzsh This is, of course, for people using zsh. I'm assuming it doesn't work for bash... Commented Jan 30, 2021 at 17:09
  • 2
    @AdolfoCorrea you can resolve the warning message by entering the following command: compaudit | xargs chmod g-w
    – Json
    Commented Sep 8, 2021 at 20:00
  • I get the annyoing message + it doesn't work... Commented Feb 28, 2022 at 8:06
  • Git completions are part of the Homebrew git package, not zsh-completions (which has completions for Git extensions, like Git Flow, among others). So, brew install git, which tells you it installs completions, and then follow Homebrew's Shell Completion guide.
    – Bluu
    Commented Mar 5, 2022 at 16:00
8

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.

2
  • 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 Commented Sep 21, 2018 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 Commented Sep 21, 2018 at 20:52
4

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

0
3

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
fi
2
  • 1
    Now I get c:8: command not found: __git_aliased_commandwhenever I try to autocomplete... Commented Feb 15, 2022 at 7:09
  • 1
    It complains about line 48 of the script: no such file or directory: _git:19: command not found: _default Commented Jan 26 at 15:09
2

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.

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

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.

2

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
    fi
    

Solution 2

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

1

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

2
  • 1
    I have done that, precisely in the hope of not having to deal with it -- still no branch suggestions after hitting tab
    – Jytug
    Commented Dec 4, 2018 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
    Commented Apr 30, 2019 at 13:58
1

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.

1

See appendix A1.7 of the Git handbook:

Zsh also ships with a tab-completion library for Git. To use it, simply run autoload -Uz compinit && compinit in your .zshrc.

0
0

If you are willing to switch to a different shell, the fish shell has this feature built in. It also has some other handy features such as autocomplete while typing.

-2

Locate your .bashrc file.

Open it and add this line at the bottom:

source /etc/bash_completion.d/git

6
  • 1
    I don't have anything an /etc/bash_completion.d/ folder, on a relatively new system (OSX 10.10.3).
    – livingtech
    Commented Jun 24, 2015 at 19:16
  • Try creating it.
    – crmpicco
    Commented Jun 25, 2015 at 8:04
  • Not bash_completion, git_completion which does different stuff. Commented Feb 19, 2016 at 22:25
  • @MichaelDurrant It works. Trust Me. ;) prowrestlingtees.com/media/catalog/product/cache/1/image/…
    – crmpicco
    Commented Feb 22, 2016 at 10:30
  • @crmpicco Could you please provide instructions for creating /etc/bash_completion.d/git? It would make your answer more valuable.
    – Dannid
    Commented Mar 16, 2016 at 17:49

You must log in to answer this question.

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