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

一个人想着一个人 提交于 2020-01-03 08:49:08

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7304120/in-my-bash-prompt-git-ps1-is-telling-me-something-is-wrong-but-what

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!