How to get git-completion.bash to work on Mac OS X?

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_profile

But now I am getting this -bash: __git_ps1: command not found everything I do a cd.

Can you please tell me what am I missing?


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