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

92

47

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?

n179911

Posted 2009-08-29T00:49:19.100

Reputation: 3 123

See also: https://trac.macports.org/wiki/howto/bash-completion

– Ioannis Filippidis – 2018-04-07T05:36:39.837

Answers

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.

Lara Dougan

Posted 2009-08-29T00:49:19.100

Reputation: 2 266

OSX latest path is /opt/local/share/git/contrib/completion/git-prompt.sh as per answer here link

– bgs – 2015-01-22T20:48:11.943

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 – 2012-07-31T12:12:12.143

note with newer versions of git/macport/bash-completion the prompt is in a different file. See: http://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 – 2012-10-22T09:24:47.120

3Because /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 – 2013-03-15T21:25:21.187

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 – 2013-10-21T20:21:13.687

1

To move to bash 4.1 or later you can follow the instructions here: http://apple.stackexchange.com/questions/70132/set-default-shell-to-macports-bash-in-mac-os-x-10-6-8

– ThomasW – 2013-12-19T07:59:50.687

56

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

grundprinzip

Posted 2009-08-29T00:49:19.100

Reputation: 661

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 – 2019-12-01T16:05:12.033

For more on __git_ps1, see http://superuser.com/a/477749/90231 answer in this stack.

– RichVel – 2014-04-19T06:40:51.330

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

iansheridan

Posted 2009-08-29T00:49:19.100

Reputation: 361

1

For those wondering how to combine that PS1 thing with colors in your prompt see: http://stackoverflow.com/questions/816790/append-gits-branch-name-to-command-prompt

– Nickolay – 2011-01-10T12:11:39.050

1More 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 – 2011-07-11T18:07:11.753

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

Andy

Posted 2009-08-29T00:49:19.100

Reputation: 131

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 – 2012-08-09T10:20:27.570

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

jtimberman

Posted 2009-08-29T00:49:19.100

Reputation: 20 109

git-completion.bash can be found here: https://raw.github.com/git/git/master/contrib/completion/git-completion.bash

– TJ Biddle – 2013-12-17T16:59:24.260

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

studgeek

Posted 2009-08-29T00:49:19.100

Reputation: 1 805

The file in question: https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh

– papercowboy – 2012-10-11T17:37:20.957

3

Or with Homebrew

brew install bash-completion git-extras

derek73

Posted 2009-08-29T00:49:19.100

Reputation: 131

2Thanks a lot. It should be brew install bash-completion git-extras though (dash instead of underscore). – simonair – 2013-02-08T15:40:41.093

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.

John Mellor

Posted 2009-08-29T00:49:19.100

Reputation: 1 311

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

haysclark

Posted 2009-08-29T00:49:19.100

Reputation: 121

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

ecloud

Posted 2009-08-29T00:49:19.100

Reputation: 111

1

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

jrc

Posted 2009-08-29T00:49:19.100

Reputation: 111

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> – r00m – 2018-08-02T20:05:20.383

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/

Taylor Edmiston

Posted 2009-08-29T00:49:19.100

Reputation: 195

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

thekingoftruth

Posted 2009-08-29T00:49:19.100

Reputation: 101

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

sufery

Posted 2009-08-29T00:49:19.100

Reputation: 39