Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

bash

rebase

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.

like image 622
Gregg Williams Avatar asked Sep 05 '11 05:09

Gregg Williams


People also ask

What is bash git prompt?

By default, the Git Bash Shell displays computer name and username properties in the prompt window. Understandably, some users prefer to change these Git Bash Shell settings. Fortunately, it's not that difficult to do. Edit the git-prompt.sh file to customize the Git Bash Shell prompt's config settings.

Where is __ git_ps1 defined?

It's /etc/git-prompt.sh that defines the __git_ps1 function (Line #273 in my version) is defined. You'll notice that the __git_ps1 function pulls in several other functions defined in /etc/git-prompt.sh .

What is __ git_ps1?

__git_ps1 can be used to create the PS1 prompt and not just its own output. If that's the case, a non-empty value tells git to color the prompt according to the current state (dirty, untracked files…) GIT_PS1_SHOWDIRTYSTATE. Shows the “dirty” indicator - meaning whether you modified tracked files.

How do I show a git branch from a bash prompt?

The git_branch() is a function, that prints the name of the current Git branch in the round brackets. We set the PS1 variable and place the function git_branch() inside it to display the Git branch in the terminal prompt. The function inside the PS1 variable must be in the following format: \$(git_branch) .


1 Answers

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

like image 64
manojlds Avatar answered Sep 20 '22 05:09

manojlds