How to get git-completion.bash to work on Mac OS X?
Solution 1
I installed git using MacPorts on my new Snow Leopard installation. After MacPorts is installed from the .dmg image, these would be the commands in Terminal.app:
sudo port selfupdate
sudo port install git-core +bash_completion
If you also want support for pulling from SVN repositories and docs, use this instead of the second line:
sudo port install git-core +bash_completion +doc +svn
Then add the following to your ~/.profile or ~/.bash_profile:
# MacPorts Bash shell command completion if [ -f /opt/local/etc/bash_completion ]; then . /opt/local/etc/bash_completion fi
or for MacPorts since version 2.1.2 on Mountain Lion:
# MacPorts Bash shell command completion if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then . /opt/local/etc/profile.d/bash_completion.sh fi
or for MacPorts with newer versions of git:
if [ -f /opt/local/share/git-core/git-prompt.sh ]; then . /opt/local/share/git-core/git-prompt.sh fi
Note: bash 4.1 or higher is required by bash_completion.sh. If completion doesn't work try echo $BASH_VERSION
to see if that's the issue. If so, enter MacPorts bash by typing bash
and try git completion again.
Solution 2
If you installed git using homebrew than you might adjust the MacPorts advice a little and add this to your .bash_profile
and .bashrc
if [ -f `brew --prefix`/etc/bash_completion.d/git-completion.bash ]; then
. `brew --prefix`/etc/bash_completion.d/git-completion.bash
fi
The best way to check if you have git correctly installed using homebrew ist to execute
brew info git
and check the output for the install directory of the git bash completion
Latest version of Git (1.7.12) also requires the following to enable the prompt.
if [ -f `brew --prefix`/etc/bash_completion.d/git-prompt.sh ]; then
. `brew --prefix`/etc/bash_completion.d/git-prompt.sh
fi
Solution 3
All you need to do is place the git-completion.bash
file in your user home bin
directory and place the following in you .profile
or .bash_profile
file:
export PATH="$HOME/bin:$PATH"
source ~/bin/git-completion.bash
PS1='[\u@\h \w$(__git_ps1 " (%s)")]\$ '
What this does is make sure that your local bin is in the PATH and the source
command gets things going. Then of course the PS1 change puts the currently checked out branch in the prompt.
So, no MacPort install to then install a the "completion" version of GIT (especially irritating if you already have it installed).
Solution 4
While the current answer is correct for older MacPorts there is a new issue with the most current MacPorts resulting in the same error.
The current bash-completion package (2.0 r1) requires at least a bash 4.1 to function properly. As a usual OS X Terminal uses /bin/bash
it only gets a 3.x. You have to change you terminal settings to use /opt/local/bin/bash -l
as described in the MacPorts documentation here: http://trac.macports.org/wiki/howto/bash-completion
Solution 5
You need to source the command completion functions. Add to your .bashrc_profile before the PS1:
. /opt/local/etc/bash_completion.d/git-completion
Related videos on Youtube
n179911
Updated on September 17, 2022Comments
-
n179911 over 1 year
I have followed http://blog.bitfluent.com/post/27983389/git-utilities-you-cant-live-without to add
git-completion.bash
to my/opt/local/etc/bash_completion.d/git-completion
and I put
PS1='\h:\W$(__git_ps1 "(%s)") \u\$ '
in my .bashrc_profileBut now I am getting this
-bash: __git_ps1: command not found
everything I do acd
.Can you please tell me what am I missing?
-
0 _ about 6 years
-
-
Nickolay over 13 years
-
Nickolay over 13 yearsFor those wondering how to combine that PS1 thing with colors in your prompt see: stackoverflow.com/questions/816790/…
-
metavida almost 13 yearsMore precisely, git-completion.bash just needs to be somewhere in your
$PATH
. I decided to put mine in/usr/local/bin/
so that I didn't have to modify my existing$PATH
. -
NullVoxPopuli almost 12 yearsI just copy pasted, and I still get the error the OP has. =\. How do you tell what's been included? I don't thenk bash_completion is sourced
-
vitch almost 12 yearsThis is the most correct answer as I only started having the problem after updating macports. The only downside to using the macports bash from terminal is that it now warns me about terminating running processes when I try to close my terminal window - have you run into this?
-
papercowboy over 11 yearsThe file in question: raw.github.com/git/git/master/contrib/completion/git-prompt.sh
-
Yehosef over 11 yearsnote with newer versions of git/macport/bash-completion the prompt is in a different file. See: trac.macports.org/ticket/36334
if [ -f /opt/local/share/git-core/git-prompt.sh ]; then . /opt/local/share/git-core/git-prompt.sh fi
-
simonair over 11 yearsThanks a lot. It should be
brew install bash-completion git-extras
though (dash instead of underscore). -
Uwe Günther about 11 yearsBecause
/opt/local/etc/profile.d/bash_completion.sh
needs$BASH_VERSION
4.1 or better, I had to do the following on a Mac OS X 10.8.3 with MacPorts 2.1.3: Switch the Mac OS X login shell from its default/bin/bash
(3.2.48(1)-release) to MacPorts/opt/local/bin/bash
(4.2.42(2)-release) like its described here: stackoverflow.com/a/791244/287984. Basicallyif [ `grep /opt/local/bin/bash /etc/shells` ]; then echo /opt/local/bin/bash | chsh -s /opt/local/bin/bash; else echo /opt/local/bin/bash | sudo tee -a /etc/shells; chsh -s /opt/local/bin/bash; fi
-
bgs over 10 yearsWorked great for me. Had to replace
/bin/bash
in Terminal.app with/opt/local/bin/bash
from macports + all the.bash_profile
changes suggested here. -
TJ Biddle over 10 yearsgit-completion.bash can be found here: raw.github.com/git/git/master/contrib/completion/…
-
ThomasW over 10 yearsTo move to bash 4.1 or later you can follow the instructions here: apple.stackexchange.com/questions/70132/…
-
Evelina about 10 yearsFor more on
__git_ps1
, see superuser.com/a/477749/90231 answer in this stack. -
roman almost 6 yearsThis was the only answer that actually worked for me. Other answers were producing strange errors when you type an incomplete command like
git br<TAB>
-
cglacet over 4 yearsWow, I didn't even knew this came along with git when installed with homebrew. They should automatically append this to
~/.bash_profile
, I wonder who wouldn't want this.