Git – (Mac) -bash: __git_ps1: command not found

gitmacosps1terminal

I'm trying to change my command promt in terminal. I keep getting the error:

-bash: __git_ps1: command not found

I've tried it just by typing it into the terminal as is: __git_ps1. I've also tried it out in the .bash_profile

if [ -f ~/.git-completion.bash ]; then
  source ~/.git-completion.bash
  export PS1='[\W]$(__git_ps1 "(%s)"): '
fi

As you might be able to see/tell, yes, I do have the auto-completion installed and it does work great!

I came across this question: " PS1 env variable does not work on mac " which gives the code

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"

So I add it to my .bash_profile hoping that it will change something. Well, it did. It just changed the error output.

Here's the .bash_profile with the addition:

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"

if [ -f ~/.git-completion.bash ]; then
  source ~/.git-completion.bash
  export PS1='[\W]$(__git_ps1 "(%s)"): '
fi

And now here's the changed error output:

sed: (%s): No such file or directory

Note: I've also moved the alias below the source with no difference. I have git version 1.7.12.1

This should be a simple change. Can someone please help me?

Edit 10/13/12

No, I definitely do not want to define __git_ps1 myself but was just trying to see if it would be recognized by doing so. Yes, I have the .git-completion.bash file installed. Here's how I got auto completion on my machine.

cd ~
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git.completion.bash ~/.git-completion.bash

A ls -la then lists the .git-completion.bash file.

Edit 10/13/12 – Solved by Mark Longair (below)

The following code worked for me in the .bash_profile while others did not…

if [ -f ~/.git-prompt.sh ]; then
  source ~/.git-prompt.sh
  export PS1='Geoff[\W]$(__git_ps1 "(%s)"): '
fi

Best Answer

You've installed the version of git-completion.bash from master - in git's development history this is after a commit that split out the __git_ps1 function from the completion functionality into a new file (git-prompt.sh). The commit that introduced this change, which explains the rationale, is af31a456.

I would still suggest that you just source the version of git-completion.bash (or git-prompt.sh) that is bundled with your installation of git.

However, if for some reason you still want to use this functionality by using scripts separately downloaded from master, you should download git-prompt.sh similarly:

curl -o ~/.git-prompt.sh \
    https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

... and add the following line to your ~/.bash_profile:

source ~/.git-prompt.sh

Then your PS1 variable that includes __git_ps1 '%s' should work fine.