Gregg Williams
Gregg Williams

Reputation: 898

In my bash prompt, $(__git_ps1) is telling me something is wrong, but what?

I've hacked my bash prompt to (in theory) tell me what git branch I'm currently in. But the output indicates that something's wrong:

22:07 (seesaw|REBASE-i) infwb $git branch
* master
  wkg

Here's the relevant code in my .bash_profile file (I'll put a larger chunck of it at the end of this question):

PS1="$GRAY\$(date +%H:%M)\$(__git_ps1) $GREEN\W$YELLOW \$"

As you can see, $(__git_ps1) returns (seesaw|REBASE-i), even though I no longer have a seesaw branch! (I did have one, and it had a rebase problem in relation to my remote seesaw branch on github. I solved the problem, I think, and git branch -r seesaw successfully removed the local copy.)

I'm pretty sure that (seesaw|REBASE-i) is telling me that something is wrong, but I don't know what it is.

Thanks for any suggestions you might have. (chunk of .bash_profile follows)

-------from .bash_profile ---------

## ----- from http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/
# Set git autocompletion and PS1 integration
if [ -f /usr/local/git/contrib/completion/git-completion.bash ]; then
  . /usr/local/git/contrib/completion/git-completion.bash
fi

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi
## ----- end 

GRAY="\[\033[1;30m\]"
YELLOW="\[\033[1;33m\]"
GREEN="\[\033[0;32m\]"

PS1="$GRAY\$(date +%H:%M)\$(__git_ps1) $GREEN\W$YELLOW \$"

----------------

ADDENDUM

@cdhowie and @manojlds, I'm impressed by your git knowledge!

git rebase --abort caused the info to change to (seesaw). Unfortunately, I can't find rebase-merge (or .git) anywhere on my hard disk. I'm on a Mac, which does Weird Things to many FOSS tools. (I did find git itself, at /usr/local/git.) Nothing at /Library/Application Support or ~//Library/Application Support, either.

(still later)

It turns out that everything is all right. Somehow the git rebase --abort caused the seesaw branch to re-appear (I wasn't expecting that!), and the command left me with seesaw as my current branch. From there, I knew what to do.

Upvotes: 8

Views: 2746

Answers (1)

manojlds
manojlds

Reputation: 301607

Based on comments with @cdhowie, I got suspicion that __git_ps1 is handling the git rebase scenario differently. So I looked into the source and found the following lines:

...
if [ -f "$g/rebase-merge/interactive" ]; then
    r="|REBASE-i"
    b="$(cat "$g/rebase-merge/head-name")"
elif [ -d "$g/rebase-merge" ]; then
    r="|REBASE-m"
    b="$(cat "$g/rebase-merge/head-name")"
...

So as long of the .git/rebase-merge exists you will be getting the "wrong" branch, even if you have moved to another branch.

git rebase --abort will fix it. Or delete .git/rebase-merge

Upvotes: 4

Related Questions