Home » Questions » Computers [ Ask a new question ]

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

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"

Asked by: Guest | Views: 358
Total answers/comments: 5
bert [Entry]

"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."
bert [Entry]

"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)."
bert [Entry]

"Annoyingly this has been broken yet again. This time its the git folks with version 1.7.12-1. To allow people to have just the prompt functionality without the completion functionality (which can slows things down), they refactored the prompt functionality out of bash_completion/git and into its own file git-prompt.sh.

So adding bash_completion as described by @Lara will only get you completion (hitting tab completes tag/branch names).

To get the __git_ps1 so you can use it in your PS1 prompt you also need to add the following to your ~/.profile.

. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh

Some background is here - bbs.archlinuxdotorg/viewtopic.php?pid=1163632#p1163632"
"Annoyingly this has been broken yet again. This time its the git folks with version 1.7.12-1. To allow people to have just the prompt functionality without the completion functionality (which can slows things down), they refactored the prompt functionality out of bash_completion/git and into its own file git-prompt.sh.

So adding bash_completion as described by @Lara will only get you completion (hitting tab completes tag/branch names).

To get the __git_ps1 so you can use it in your PS1 prompt you also need to add the following to your ~/.profile.

. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh

Some background is here - bbs.archlinuxdotorg/viewtopic.php?pid=1163632#p1163632"
bert [Entry]

"Or with Homebrew

brew install bash-completion git-extras"
bert [Entry]

"Found a solution on a similar question:

The git-osx-installer comes with git-completion.bash and installs it to /usr/local/git/contrib/completion/. Just source that file from your .bash_profile to get __git_ps1."