Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have followed 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?

share|improve this question

13 Answers 13

up vote 65 down vote accepted

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

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

or for MacPorts since version 2.1.2 on Mountain Lion:

# MacPorts Bash shell command completion
if [ -f /opt/local/etc/profile.d/ ]; then
  . /opt/local/etc/profile.d/

or for MacPorts with newer versions of git:

if [ -f /opt/local/share/git-core/ ]; then
    . /opt/local/share/git-core/

Note: bash 4.1 or higher is required by 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.

share|improve this answer
Thank you, that definitely helped! – NiKo Apr 30 '12 at 12:03
I 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 – NullVoxPopuli Jul 31 '12 at 12:12
note with newer versions of git/macport/bash-completion the prompt is in a different file. See: if [ -f /opt/local/share/git-core/ ]; then . /opt/local/share/git-core/ fi – Yehosef Oct 22 '12 at 9:24
Because /opt/local/etc/profile.d/ 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: Basically if [ `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 – Uwe Günther Mar 15 '13 at 21:25
To move to bash 4.1 or later you can follow the instructions here:… – ThomasW Dec 19 '13 at 7:59

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

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/ ]; then
    . `brew --prefix`/etc/bash_completion.d/
share|improve this answer
Exactly what I was looking for. Thanks! – Trey Piepmeier Sep 29 '12 at 2:46
For more on __git_ps1, see answer in this stack. – RichVel Apr 19 '14 at 6:40

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).

share|improve this answer
For those wondering how to combine that PS1 thing with colors in your prompt see:… – Nickolay Jan 10 '11 at 12:11
More 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. – metavida Jul 11 '11 at 18:07

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:

share|improve this answer
This 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? – vitch Aug 9 '12 at 10:20

You need to source the command completion functions. Add to your .bashrc_profile before the PS1:

. /opt/local/etc/bash_completion.d/git-completion
share|improve this answer
git-completion.bash can be found here:… – TJ Biddle Dec 17 '13 at 16:59

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

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/

Some background is here -

share|improve this answer
The file in question: – superclint Oct 11 '12 at 17:37

Or with Homebrew

brew install bash-completion git-extras
share|improve this answer
Thanks a lot. It should be brew install bash-completion git-extras though (dash instead of underscore). – simonair Feb 8 '13 at 15:40

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.

share|improve this answer

For Mountain Lion and Mac-Ports I needed to use the following lines in my .bash_profile to get both __git_ps1 and completions to run normally:

# Bash shell command completion
if [ -f /opt/local/share/doc/git-core/contrib/completion/git-completion.bash ]; then
  . /opt/local/share/doc/git-core/contrib/completion/git-completion.bash

if [ -f /opt/local/share/doc/git-core/contrib/completion/ ]; then
  . /opt/local/share/doc/git-core/contrib/completion/
share|improve this answer

I use brew, so a master bash_completion script which runs all the ones in bash_completion.d seems not to be provided. The solutions which propose just running a single script to add git completions aren't extensible, because different packages will add different completion scripts, and you probably want to run them all. Therefore I added this (adapted from Arch Linux) to the end of /etc/bashrc:

# source compat completion directory definitions
    for i in $(LC_ALL=C command ls "$BASH_COMPLETION_COMPAT_DIR"); do
        [[ -f $i && -r $i ]] && . "$i"
share|improve this answer

In addition to Adam K. Johnson's post

You can also put the following...

# MacPorts Bash shell command completion
if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi your /etc/profile, if you want.

That way all users on the system will benefit from any bash completion scripts installed in the directory "/opt/local/etc/bash_completion.d/".

share|improve this answer

At least with Xcode 6, you already have git-completion.bash. It's inside the Xcode app bundle.

Just add this to your .bashrc:

source `xcode-select -p`/usr/share/git-core/git-completion.bash
share|improve this answer

I think you maybe forget sourcing the file in your login .bashrc, you can add something like:

. ./path/to/your/file.ext

Using . followed by a space runs the file in the current context.

share|improve this answer

You must log in to answer this question.

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