Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

12 Answers 12

up vote 61 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 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.

share|improve this answer
1  
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: 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 Oct 22 '12 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 –  Uwe Günther Mar 15 '13 at 21:25
1  
To move to bash 4.1 or later you can follow the instructions here: apple.stackexchange.com/questions/70132/… –  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
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
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 superuser.com/a/477749/90231 answer in this stack. –  RichVel Apr 19 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
2  
1  
For those wondering how to combine that PS1 thing with colors in your prompt see: stackoverflow.com/questions/816790/… –  Nickolay Jan 10 '11 at 12:11
1  
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: http://trac.macports.org/wiki/howto/bash-completion

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: raw.github.com/git/git/master/contrib/completion/… –  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 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

share|improve this answer
    
The file in question: raw.github.com/git/git/master/contrib/completion/git-prompt.sh –  superclint Oct 11 '12 at 17:37

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
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
share|improve this answer

Or with Homebrew

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

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

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.