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
1
  • 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! Commented Oct 19, 2017 at 21:28

6 Answers 6

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.

6
  • I did exactly what you said, Tony, but it still does not work Commented Mar 1, 2017 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. Commented Mar 2, 2017 at 21:08
  • Installing git with brew install git solved my problem. Thanks @TonyWilliams Commented Nov 13, 2017 at 9: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
    Commented Nov 22, 2017 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
    Commented Sep 3, 2018 at 9:07
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
4
  • I'm not entirely sure why this was downvoted when it is both correct and relevant to Catalina, which uses zsh. Commented Feb 13, 2020 at 22:12
  • Where these files are supposed to be stored? Commented Jul 7, 2020 at 12:39
  • 2
    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. Commented Aug 18, 2020 at 14:38
  • Getting not found: __git_aliased_command when trying branch name autocomplete :/ basic commands autocomplete, e.g. for checkout works though. Commented Feb 9, 2022 at 3:22
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.

2
  • This solved my problem. Thank you very much :) Commented Oct 13, 2021 at 16:13
  • 1
    This should be the accepted answer, and the first result to appear on Google :) .. thanks man
    – AbdelHady
    Commented Jan 6, 2023 at 16:14
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.

2
  • 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. Commented Aug 18, 2020 at 14:39
  • 2
    This results in ERROR: this script is obsolete, please see git-completion.zsh
    – Qwertie
    Commented Nov 24, 2020 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"
2
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

You must log in to answer this question.

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