Homebrew’s `git` not using completion

MacOS

Question or issue on macOS:

When using OSX’s git, after I modify a file I can simply do git commit , and that’ll auto complete the file’s name to the one that was modified. However, if I install a newer version of git from homebrew and I use it, that feature no longer works (meaning I press and it just “asks” me what file I want to do it on, even including the ones that have no changes).

Can anyone shed some light as to why, and how to solve that? I’d prefer using homebrew’s git, since it’s more up-to-date.

My shell is zsh, and Neither installing bash-completion or zsh-completions worked (even after following homebrew’s post-install instructions).

Also, after installing git with homebrew it says

Bash completion has been installed to: /usr/local/etc/bash_completion.d
zsh completion has been installed to: /usr/local/share/zsh/site-functions

So shouldn’t I be able to use one of those?

How to solve this problem?

Solution no. 1:

You’re looking for:

brew install git bash-completion

As warpc’s comment states, you’ll need to add the following to your ~/.bash_profile to get homebrew’s bash-completion working:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

The above is mentioned in the caveats when you install the bash-completion formula.


Note: if you are using Bash v4 or later (via brew install bash) then you’re going to want to use brew install [email protected], to enable tab completion add the following to ~/.bash_profile as described in the caveats:

export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

The additional export is necessary for git, docker, youtube-dl, and other completions which may be included in the $(brew --prefix)/etc/bash_completion.d/ directory.

Solution no. 2:

This get’s git tab completion working on OSX without having to restart your terminal:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash && echo "source ~/.git-completion.bash" >> ~/.bash_profile && source ~/.bash_profile

Solution no. 3:

In case anyone else makes my dumb mistake, try brew install git. I was using the git that comes with Xcode and didn’t realize that I had never installed Homebrew’s git to get the autocompletions.

Solution no. 4:

for some reason I was missing the file at $(brew --prefix)/etc/bash_completion so @Graham Perks’ correct answer didn’t work for me

It ended up the fix in my case was:

brew unlink bash-completion
brew link bash-completion

Solution no. 5:

I solved the problem by figuring out that $(brew --prefix)/etc/bash_completion returned Permission denied when executed. So after a simple:

chmod +x $(brew --prefix)/etc/bash_completion

Everything is now working fine. I’m wondering why Homebrew doesn’t make the bash_completion file executable on installation, though.

Solution no. 6:

Found a working solution. It’s very recent (authored 16 hours ago, and committed 2 hours ago), and it comes directly from homebrew.

brew install git --without-completions

Just tried it, and it finally works as intended.

Solution no. 7:

I had the same issue and even found this post this morning. I fixed the issue by updating brew with brew update and then reinstalling git with brew reinstall git.

I was then notified of another file that is blocking the homebrew linking process, in my case it was /usr/local/share/zsh/site-functions/git-completion.bash. Removing the file and running brew link git solved the issue. Guessing it was just a bad recipe version we stumbled upon.

Solution no. 8:

If you have $BASH_VERSION < 4.1, eg 3.2.57(1)-release then go ahead with:

brew install bash-completion
# In ~/.bash_profile :
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

However if you’ve brew install bash to get version 4.4.12(1)-release
you can use the better and more complete completions in:

brew install [email protected]
# In ~/.bash_profile:
[ -f "$(brew --prefix)/share/bash-completion/bash_completion" ] \
&& . "$(brew --prefix)/share/bash-completion/bash_completion"

Note that some packages (brew, docker, tmux) will still put some completions into $(brew --prefix)/etc/bash_completion.d/ so you might add:

for completion in "$(brew --prefix)/etc/bash_completion.d/"*
do
    . $completion
done

Finally you should be able to add the git completion script if for some reason the way you installed git did not add it to either of those:

[[ -f $(brew --prefix)/etc/bash_completion.d/git \
|| -f $(brew --prefix)/share/bash-completion/completions/git ]] \
|| curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash \
    -o $(brew --prefix)/etc/bash_completion.d/git

You can get and add it with the above.

Solution no. 9:

Step 1: Download auto completion script:

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

Step 2: Update .bash_profile and .bashrc

echo "source ~/git-completion.bash" >> .bash_profile

Via https://www.anintegratedworld.com/git-tab-autocomplete-on-osx-10-11-el-capitan/

If above does not work, try https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion

Solution no. 10:

In 2019, using Bash v5, you do not need to explicitly source the git bash completion script in your .bash_profile or .bashrc

  1. Ensure you have the following two lines in your .bashrc
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
  1. Download the git bash completion script (https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash) and save it to /usr/local/etc/bash_completion.d/ as git

That’s it! Bash will automatically pick up the git completion file and enable completion.


Side Note: I recommend putting all these changes in .bashrc as this ensures that when you drop into an interactive shell (ie. from pipenv shell), completions will get loaded correctly as bash will source .bashrc and NOT .bash_profile.

Hope this helps!