96

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?

1

14 Answers 14

64

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.

6
  • 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 Commented Jul 31, 2012 at 12:12
  • note 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
    – Yehosef
    Commented Oct 22, 2012 at 9:24
  • 3
    Because /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. 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 Commented Mar 15, 2013 at 21:25
  • Worked 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.
    – bgs
    Commented Oct 21, 2013 at 20:21
  • 1
    To move to bash 4.1 or later you can follow the instructions here: apple.stackexchange.com/questions/70132/…
    – ThomasW
    Commented Dec 19, 2013 at 7:59
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
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
2
  • For more on __git_ps1, see superuser.com/a/477749/90231 answer in this stack.
    – RichVel
    Commented Apr 19, 2014 at 6:40
  • Wow, 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.
    – cglacet
    Commented Dec 1, 2019 at 16:05
26

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

3
13

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

1
  • 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
    Commented Aug 9, 2012 at 10:20
6

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

. /opt/local/etc/bash_completion.d/git-completion
1
5

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 - https://bbs.archlinux.org/viewtopic.php?pid=1163632#p1163632

1
4

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
1
  • This 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>
    – roman
    Commented Aug 2, 2018 at 20:05
4

Or with Homebrew

brew install bash-completion git-extras
1
  • 2
    Thanks a lot. It should be brew install bash-completion git-extras though (dash instead of underscore). Commented Feb 8, 2013 at 15:40
2

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.

2

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
fi

if [ -f /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh ]; then
  . /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
fi
1

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
BASH_COMPLETION_COMPAT_DIR=/usr/local/etc/bash_completion.d
if [[ -d $BASH_COMPLETION_COMPAT_DIR && -r $BASH_COMPLETION_COMPAT_DIR && \
    -x $BASH_COMPLETION_COMPAT_DIR ]]; then
    for i in $(LC_ALL=C command ls "$BASH_COMPLETION_COMPAT_DIR"); do
        i=$BASH_COMPLETION_COMPAT_DIR/$i
        [[ -f $i && -r $i ]] && . "$i"
    done
fi
0

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

...in 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/".

0

While the answer above by grundprinzip from 2011 still works fine, I wanted to add a more recent answer now that there's now a Homebrew package bash-completion (formula, repo) that makes this even easier. (This includes completion for git as well as other common tools.)

Today macOS ships with Bash 3, but you can brew install bash to get Bash 4. If you're running Bash 4, then install the newer 2.x branch of bash-completion.

Bash 4

  1. Run:

    brew install bash-completion@2
    
  2. Add to ~/.bash_profile:

    if [ -f /usr/local/share/bash-completion/bash_completion ]; then
      . /usr/local/share/bash-completion/bash_completion
    fi
    

Bash 3

If you want to stick with stock bash 3.x on macOS then follow these directions instead.

  1. Run:

    brew install bash-completion
    
  2. Add to ~/.bash_profile:

    echo "[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion" >> ~/.bash_profile
    

Reference: http://davidalger.com/development/bash-completion-on-os-x-with-brew/

-2

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.

You must log in to answer this question.

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