23

I have tried the examples found here to get Git auto-completion working on El Capitan, however none of the examples work for me.

I installed bash-completion with Homebrew (brew install bash-completion).

This is my ~/.bash_profile currently (as you can see, i've tried a few things):

CRMPiccosMacBook:etc crmpicco$ cat ~/.bash_profile 
alias ll='ls -lG'
alias composer="php /usr/local/bin/composer.phar"

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

if [ -f `brew --prefix`/etc/bash_completion.d/git-completion.bash ]; then
    . `brew --prefix`/etc/bash_completion.d/git-completion.bash
fi

#source /usr/local/git/contrib/completion/git-completion.bash
#GIT_PS1_SHOWDIRTYSTATE=true
#export PS1='[\u@mbp \w$(__git_ps1)]\$ '

So, if I go into my directory with the cloned Git repo and type git checkout m and hit [TAB] I get nothing (when expecting master to be pre-filled).

When I run a source on it, it doesn't exist - however it's installed.

CRMPiccos-MacBook:signup crmpicco$ brew install bash-completion
Warning: bash-completion-1.3_1 already installed
CRMPiccos-MacBook:signup crmpicco$ source /etc/bash_completion.d/git-completion.bash
-bash: /etc/bash_completion.d/git-completion.bash: No such file or directory
crmpicco
  • 1,081
  • 6
  • 17
  • 26
  • Tony Williams' answer here looks solid and this used to work for me on my Mac, I believe. Not sure when or why it stopped working on my Mac and now Tony's advice also didn't fix it for me. However, Michael Durrant's advice here did! – Matthew Kuraja Oct 19 '17 at 21:28

6 Answers6

28

You are confusing the basic bash completion with the add on required for completing git commands.

The git that is installed by Apple lacks the required git-completion.bash file so you need to install the full git. You can do this easily with homebrew -brew install git will do the job.

Once you've done that then uncomment your top three lines :-

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

Now source ~/.bash_profile and it should work fine.

pkamb
  • 8,791
Tony Williams
  • 12,142
  • I did exactly what you said, Tony, but it still does not work – Arthur Eirich Mar 01 '17 at 15:55
  • 4
    The steps you need are :- (1) Install bash-completion using homebrew (2) install git using homebrew. (3) add the lines above to your .bash_profile. Now open a new Terminal window and you should find git completion working. – Tony Williams Mar 02 '17 at 21:08
  • Installing git with brew install git solved my problem. Thanks @TonyWilliams – Serhii Holinei Nov 13 '17 at 09:34
  • 4
    I found with Homebrew git version 2.15.0 the bash completion script path is slightly different, so \brew --prefix`/git/contrib/completion/git-completion.bash` – javabrett Nov 22 '17 at 05:05
  • 1
    In my case, git version: 2.18.0, the file was in $(brew --prefix)/etc/bash_completion.d/git-completion.bash – tokosh Sep 03 '18 at 09:07
  • I had to install brew install git bash-completion then adding if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi

    to bash_profile.

    – Gagan Sep 23 '19 at 12:04
11

As a note for users, like me, who already updated to macOS Catalina, which deprecated bash, yet ended up at this answer as a top result...

For macOS Catalina+, which uses zsh, there are a few other requirements. Download both scripts:

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

Then update your ~/.zshrc with:

zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.bash
fpath=(~/.zsh $fpath)

autoload -Uz compinit && compinit
8

Enable Git Tab Autocomplete for Zsh

New Macs use the Zsh shell by default. If you’re using Zsh, add the following line to the ~/.zshrc file and restart your Terminal application:

autoload -Uz compinit && compinit

Alternatively, you can run the following two commands in your Terminal application to add the necessary line to the .zshrc file and restart your shell.

echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
source ~/.zshrc

Git tab autocomplete is now enabled on your Mac.

Wolfack
  • 181
1

With the help of this answer I solved it using these commands

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

Then I edited the file by running vi ~/.zshrc and added following part to it

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

Then I did run source ~/.zshrc then git suggestions started working properly.

  • The title of this question refers to Catalina, but a careful reader will note that the detail in the question refers to bash. Therefore, a zsh answer misses the question's point. – John Anderson Aug 18 '20 at 14:39
  • 2
    This results in ERROR: this script is obsolete, please see git-completion.zsh – Qwertie Nov 24 '20 at 23:46
1

For people using bash 5, you can install the homebrew package bash-completion@2 and add the following to your ~/.bash_profile.

[[ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]] && . "$(brew --prefix)/etc/profile.d/bash_completion.sh"
0

Here's what worked for me on Sierra. Found here.

After installing bash-completion and adding the snippet to your .bash_profile:

cd /usr/local/etc/bash_completion.d  
ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion  
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion  
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion
Pixy
  • 101