Homebrew’s `git` not using completion
Asked Answered
K

20

179

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?

Kermit answered 20/2, 2013 at 1:24 Comment(4)
What version of zsh and git are you using?Chartist
Latest from homebrew. zsh 5.0.2 and git 1.8.1.3Kermit
Do other git completions work (git co<TAB> ==> git commit, for example)?Chartist
I suspect a few different issues end up here, but installing git via brew fixed this for me. Seems zsh-completions wasn't happy with the mac git, though it worked fine before (possibly updated blindly to 0.17.x).Barye
L
335

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.

Lumper answered 20/2, 2013 at 1:47 Comment(18)
My shell is zsh. Does that mean I should do brew install git zsh-completions? 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 couldn't I use one of those?Kermit
Neither installing bash-completion or zsh-completions worked (even after following homebrew's instructions after install).Kermit
Did you restart your terminal?Lumper
Did you also add in ~/.bash_profile for bash case this content: if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fiOcelot
I have already installed git without brew. Now I want to install bash_completion using brew. I ran brew install bash-completion and added above code but no luckPastelki
I'm confused by the mention of bash-completion here, because bash-completion does not provide git-completion. Perhaps people with unsolved problems could try adding it manually?Agamemnon
For git completion to work, per github.com/git/git/blob/master/contrib/completion/… , . ~/bin/git-completion.bash is also needed. So (using a semi-colon where this comment format removes line breaks): ``` if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi; . ~/bin/git-completion.bash ```Current
@Current git-completion.bash is automatically included, since it's in /usr/local/etc/bash_completion.d. Everything in bash_completion.d gets loaded by the bash_completion script (you can verify this by searching the script for BASH_COMPLETION_DIR).Lumper
@Pastelki Any luck in your case ? I am facing the similar scenario atm. Any help in this regard?Northumberland
@vyper did you 'source ~/.bash_profile' after adding the extra line? (Or kill and re-open your Terminal)Lumper
@GrahamPerks yup tried both the things. The problem is i am using Apple's git that comes default with osx and i am using brew bash completion. And due to some weird reason brew bash completion aint working with Apple git.Northumberland
On the contrary curl raw.githubusercontent.com/git/git/master/contrib/completion/… -o ~/.git-completion.bash && echo "source ~/.git-completion.bash" >> ~/.bash_profile && source ~/.bash_profile this actually worked for me. I have no clue what extra bash completion provides with git-completion.bash doesn't provide.Northumberland
I was scratching my head until I realised it's git bash-completion, not just bash-completion.Suspicion
It's worth noting that installing git through brew is essential for this to work. I had not known that the apples version of git was part of the problem. I downvoted this answer before I knew that and now I can't upvote :(Ric
brew install git bash-completion does not work. It took 10 to 15 minutes for it to download and compile on my Mac using all cores to do so! Result: completion still does not work. Further, the installation broke my prompt's usage of __git_ps1, with the result: -bash: /usr/local/etc/bash_completion.d/git-prompt.sh: No such file or directory Now I have to go fix everything it messed up.Solberg
i found the solution link hereWestbrooke
Weird. For me, I was using Bash v4+, but bash-completion@2 completely did not work for me (e.g. git branches). I made sure bash-completion was unlinked and bash-completion@2 was linked. I ended up having to stick with bash-completion (unlinked bash-completion@2 and linked bash-completion`).Engross
For Apple Sillicon Macs the paths should all be /opt/homebrew instead of /usr/local, or better yet use $HOMEBREW_PREFIX which is added by brew shellenvKellsie
P
124

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/

Peden answered 24/1, 2015 at 19:25 Comment(11)
It’s a bit of a cheat to call it a one-liner when using &&. Any bash script could be a one-liner like that.Kermit
You are the true MVP!Decanal
Definitely better than brew install method. Simply because it works.Polyzoan
Been searching for hours on a solution to as to why bash wasn't picking up the git-completion commands. Finally, this worked! Thank you so much!Alectryomancy
If you're like me who tried to install git autocomplete through Homebrew many times over the course of months with no success, this is the light.Farci
This is the actual solution more easyKeeley
Works like charm! ThanksAmylopsin
HOURS of trying to fix the Homebrew stuff on Catalina, looked at this and thought, PFFFFFF, yeah right! BUT IT WORKED! No idea why, already did 12 things like it, but this just worked. Oh well... DO IT!!! (Also, thanks!!! :D)Frantic
HEADS UP: if you're new at this stuff... make DANG SURE you have 2 of these >> because just 1 would overwrite your whole ~/.bash_profile and that's not great :PFrantic
source ~/.bash_profile > "ERROR: this script is obsolete, please see git-completion.zsh"Aviary
@Aviary I get this error when using a zsh shell but not using bash. What's your default shell?Peden
W
27

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.

Woodhead answered 20/12, 2016 at 21:0 Comment(0)
R
19

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
Ramsdell answered 2/7, 2014 at 11:50 Comment(2)
After following @Graham Pixel's answer, this finally made it wok for me.Macroscopic
This also just worked for me with Homebrew 1.6.0, git 2.17.0 (hub 2.2.9), and bash-completion 1.3 on OS X Sierra 10.12.6.Westward
B
13

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.

Barbershop answered 18/8, 2015 at 22:33 Comment(1)
No, this is wrong. bash_completion is meant to be source'd so the definitions are in your shell; executing will have no effect.Pedalfer
W
6

For bash on macOS Catalina (3/30 update: Big Sur too), if you want to also use Bash 5 from homebrew, you need to make sure that your login shell is set to homebrew's bash, and not the default.

To check if you need to do this, run echo ${BASH_VERSION}. If you see a version starting with 3, you are not using Brew's bash for your login shell.

To change this,

  1. Open System Preferences->Users and Groups.
  2. Right click your user and select "Advanced Options". You may need to unlock this with your password by clicking the lock in the bottom left.
  3. Set the login shell field to the location of your brew's bash, which you can usually find by running which bash in a terminal after you install brew's bash. Mine was /usr/local/bin/bash.

Restart your terminal, and follow the instructions in this excellent answer

Wordbook answered 29/10, 2020 at 17:10 Comment(2)
I caution you to not change your user default login shell. Why? If you set your login shell to a "custom" install via any tool -- e.g. Homebrew, you risk breaking your default terminal if/when you uninstall or are reinstalling Homebrew or other said tool. It is much better to use an alternate terminal such as iTerm2 and set your custom login shell for that tool only. This way when you have an issue you can fall back to the system default Terminal and user shell.Orlon
This answer worked for me on a Macbook M1 running MontereyGuffaw
J
5

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.

Joachim answered 2/3, 2013 at 16:34 Comment(3)
You'll also notice the version of git you are using is Apple's and not the version installed by Homebrew. At least in my case I did. You can check the version with git --versionJoachim
Nope. That still does not work for me. I've always gotten back to using OSX's git, that completion is just too big a feature to let go.Kermit
git-completion stopped working for me after update to Mavericks. I had to brew reinstall git and also brew reinstall bash-completion to make it work again.Villanelle
K
4

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.

Kermit answered 2/3, 2013 at 21:9 Comment(7)
I don't see how installing git without completion could improve the situation of completion not working. I believe Graham Perks' answer https://mcmap.net/q/136380/-homebrew-s-git-not-using-completion should be flagged as the correct answer.Mcgarry
I had a similar issue and I've solved reinstalling git --without-completions and sourcing the bash-completion.bash file from .bash_profile that most probably is overridden following the standard procedure.Gerda
This is not a correct answer. @graham perks's answer is correct.Overturf
@Overturf Graham’s answer did not solve my issue, this did, that’s I why I marked it.Kermit
Agree 100% with user137369: I already had homebrew's bash-completion package installed so @graham's answer couldn't possibly work for me. However, brew install git fixed everything (no need for --without-completions). I think this is also a correct answer.Crocein
This answer needs some more explaining, how can completion work if you install it without?Duenna
Seeing as brew install git now works without the need for either this or @Graham Perk’s solution, I’ll mark his as accepted, instead. It was likely some issue at the time that has since been resolved.Kermit
I
3

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

Imitate answered 28/9, 2016 at 6:41 Comment(1)
@mani9418 if you use latest macOS, try add the line into .bashrcImitate
S
3

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 bash-completion@2
# 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.

Subjugate answered 9/3, 2018 at 8:19 Comment(0)
D
2

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.

Driest answered 9/10, 2019 at 16:58 Comment(0)
B
1

For me , I had to put

source $(brew --prefix)/etc/bash_completion

into .bashrc (not .bash_profile) to get this to work.

".bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells" -- from What is the difference between .bash_profile and .bashrc? It appears to me that MacOS Sierra doesn't execute .bash_profile when opening a new terminal window, only .bashrc.

I wouldn't put it in _bash_profile, because then I'd have to reboot/logout for updates to take effect.

Brook answered 30/10, 2017 at 14:13 Comment(0)
H
1

This worked for me in Mojave (OSX 10.14.1):

brew install bash-completion

Then add the following line to your ~/.bash_profile:

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
Hexapla answered 7/11, 2018 at 19:12 Comment(1)
It just multiplies the number of warningsHomunculus
A
1

If nothing works, it could be because you have an older version of bash and bash completion script is not getting sourced by the /usr/local/etc/profile.d/bash_completion.sh script. You can test this by adding a simple echo inside the conditionals in file /usr/local/etc/profile.d/bash_completion.sh:

 10         if shopt -q progcomp && [ -r /usr/local/Cellar/bash-completion@2/2.11/share/bash-completion/bash_completion ]; then
 11             # Source completion code.
                echo "doing bash completion or not"
 12             . /usr/local/Cellar/bash-completion@2/2.11/share/bash-completion/bash_completion

And open a new terminal. If you don't see the echo message, then the conditionals do not evaluate to true. In my case it was because the bash version was old, the default from mac 3.2.blah.

I did install a newer bash from brew, but i forgot to chsh and that caused me a lot of headache. bash --version would return 5.1.8 but the enabled shell was still the old one :) To test the enabled bash you can do

for n in {0..5}
do
  echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
done

The fix was to sudo chsh -s /usr/local/bin/bash After which the completions worked.

Antre answered 13/6, 2021 at 6:4 Comment(0)
S
0

It may have something to do with libedit being used instead of readline in Lion.

Try installing readline before git.

brew install readline
Seminarian answered 20/2, 2013 at 1:41 Comment(0)
N
0

For those who already have brew bash-completion installed. I did not have the git completion script installed and could not find any tap for that.

So I added it manually:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o $(brew --prefix)/etc/bash_completion.d/git

Note that you have to rename the file and remove the extension for it to work.

If you do not have completion or git installed, install it in the accepted answer.

brew install git bash-completion

Nickels answered 14/5, 2017 at 12:21 Comment(0)
S
0

If you used homebrew to install git, then probably there is no need to install anything to support git completion. "git-completion.bash" file is somewhere (mine was here: /usr/local/git/contrib/completion/git-completion.bash)

All you need to do is to find the file: sudo find / -type f -name "git-completion.bash"

Then source its path in your .bash_profile. For example I needed to add this line to my ~/.bash_profile:

source /usr/local/git/contrib/completion/git-completion.bash

Don't forget to source your ~/.bash_profile or reopen your terminal ;)

from: how-enable-git-tab-completion-bash-mac-os-x

Sophocles answered 24/12, 2018 at 17:0 Comment(1)
@Kermit it does - cause here you do not need to install stuff that was already installed...Sophocles
C
0

I know this is an old post, but you don't really need to install any additional packages.

Homebrew is informing you that there is a directory with all the stuff you need.

You can simply add the following line to your .bash_profile if you are using Bash:

source /usr/local/etc/bash_completion.d/git-completion.bash
Cannon answered 13/5, 2019 at 17:2 Comment(0)
F
0

After tearing my hair out over this one for ages, I discovered that when I had the hub command installed, the completions for the hub command were breaking the completions for git. I had to remove /usr/local/etc/bash_completion.d/hub.bash_completion.sh. This meant no completions for hub, but git completions now worked. I didn't debug why this was.

I had the following brew packages installed:

  • bash: 5.1.16
  • bash-completion@2: 2.11
  • git: 2.35.1
  • hub: 2.14.2
Flitter answered 12/4, 2022 at 21:12 Comment(1)
This is what bit me too, everything working with my bash-completion setup in general so completions worked for all other executables except for git, and hub's completion script was the culprit. There's an issue for it but three years later I still haven't turned my manual fix into a proper patch 😅 github.com/mislav/hub/issues/2684#issuecomment-754664270Proportionable
L
-1

Enable Auto Completion of GIT commands on MAC-OS Mojave 10.14 I am a developer and use GIT from the command line all of the time. When I consider the development perspective, I used to execute a lot of commands using the command line for GIT operations. Most of the time it is very annoying that MAC OS doesn't have automatic support for the command completion which I partially support. as well as the command suggestions, which means what are the commands available for typed characters. So it is very troublesome to type very long command and mostly repetitive task as typo going wrong. :(

Tab completion would certainly be faster and easier. Unfortunately, the default installation of git on some Mac computers doesn't have tab completion enabled.

So that I was searching for a fix for the problem and there are several solutions found from the web search such as StackOverflow, GitHub as well as from the medium. Unfortunately, those solutions did not work for me and got frustrated with trying different solutions so many times.

I was searching deeply and trying out different solutions and fortunately, it is an easy fix. Below are the steps I have collected from several posts and finally it worked as expected. So I hope to share with others who have this problem like me.

f you go to the web search and you can find many solutions which mentioned the git completion bash file. Even GitHub guide as well. But I suggest you check first if the git-completion.bash file is already in your MAC computer with the git-core or something else which came from installation. you can use below command.

sudo find / -type f -name "git-completion.bash"

you will get below results. (may have some difference according to the content)

/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
/Users/Dilanka/git-completion.bash
/Users/Dilanka/.oh-my-zsh/plugins/gitfast/git-completion.bash
/Users/Dilanka/Downloads/git-completion.bash

I suggest you to pick which installed from git-core

If the git-completion.bash script doesn't exist on your machine, please retrieve it from the below provided above and save it to your local machine in a new file called git-completion.bash in the /usr/local/etc/bash_completion.d/ directory.

https://git-scm.com/book/en/v1/Git-Basics-Tips-and-Tricks

If you use the Bash shell, Git comes with a nice auto-completion script you can enable. Download it directly from the Git source code at

https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

If the git-completion.bash script exists on your machine, but is not in the /usr/local/etc/bash_completion.d/ directory, you should create that directory and copy the file into it. Below command will do the job:

sudo mkdir /opt/local/etc/bash_completion.d
sudo cp /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash /usr/local/etc/bash_completion.d/git-completion.bash

After the completion of above. The git-completion.bash script should exist on your local machine in the/usr/local/etc/bash_completion.d/ directory.

Now you need to refresh your profile using below command. It will load your added bash file to the terminal context.

source ~/.bash_profile

Great!!! you have done it. Just start the terminal window and try it. Just type "git sta" it will show suggestions as below:

git sta
stage    stash    status
git chec<TAB> will show git checkout

see my GitHub post here:

https://github.com/DIL8654/Enable-Auto-Completion-of-GIT-commads-on-MAC-OS-Mojave

See my Medium post here:

https://medium.com/@dilanka85/enable-auto-completion-of-git-commands-on-mac-os-mojave-10-14

Linet answered 5/1, 2019 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.