14

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
  • 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
25

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.

| improve this answer | |
  • I did exactly what you said, Tony, but it still does not work – Arthur Eirich Mar 1 '17 at 15:55
  • 3
    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 2 '17 at 21:08
  • Installing git with brew install git solved my problem. Thanks @TonyWilliams – Sergey Goliney Nov 13 '17 at 9:34
  • 3
    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 5: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 3 '18 at 9:07
4

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
| improve this answer | |
  • I'm not entirely sure why this was downvoted when it is both correct and relevant to Catalina, which uses zsh. – NonCreature0714 Feb 13 at 22:12
  • Where these files are supposed to be stored? – Ganesh Satpute Jul 7 at 12:39
  • 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 at 14:38
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.

| improve this answer | |
  • 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 at 14:39
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
| improve this answer | |

You must log in to answer this question.

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