Homebrew’s `git` not using completion

When using OSX’s git, after I modify a file I can simply do git commit <tab>, 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 <tab> 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?


Solution 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 bash-completion@2, 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 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

EDIT: this doesn't work in Catalina's default zsh shell. I changed the default shell back to bash and it works again. https://www.howtogeek.com/444596/how-to-change-the-default-shell-to-bash-in-macos-catalina/

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