6

Using GitBash with Git for Windows, my PS1 variable is set to display current branch as such:

$ echo $PS1
\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$

This works fine to start, for example my prompt starts out like

myuser@mypc MINGW64 /c/git/myrepo (master)
$

However, after I run any git command, the current branch portion is omitted until I restart the shell.

myuser@mypc MINGW64 /c/git/myrepo (master)
$ git branch
* master
  myOtherBranch

myuser@mypc MINGW64 /c/git/myrepo
$

But if I run __git_ps1 it still tells me the correct value:

myuser@mypc MINGW64 /c/git/myrepo
$ echo `__git_ps1`
(master)

myuser@mypc MINGW64 /c/git/myrepo
$

Any ideas what could be happening, or how I could go about diagnosing/fixing this issue?

6
  • I have the same PS1 value and don't observe this behavior. After you run a git command but before restarting the shell (i.e. while it is not showing the branch name), have you verified that the PS1 value is still as you expect? (It shouldn't change, but perhaps an alias is set up that's doing something it shouldn't; is there someone that might have had access to your profile, that might be playing a little joke?) Commented Mar 10, 2017 at 15:30
  • I did, yes. The value is identical before and after running a command. Another fact I should probably mention is that this was working fine earlier, but stopped working after upgrading from version 2.8.0.windows.1 to 2.12.0.windows.1. Commented Mar 10, 2017 at 15:43
  • Just did a test upgrade of one of my systems to 2.12.0.windows.1 (64bit) and still can't reproduce the issue. Version might be a factor, but there must be more to it. Commented Mar 10, 2017 at 17:50
  • I'm not really surprised - if everyone was seeing it there would be results in Google search. I guess as a Bash noob I was wondering if anyone knew a way to get some diagnostics on it to dig deeper. Commented Mar 13, 2017 at 13:08
  • Rolled back to previous version of Git and things are working fine again. I don't really need the upgrade so I'm not going to investigate anymore. Commented Mar 14, 2017 at 16:31

4 Answers 4

6

So I was able to get things working again based off this and the info already here in this question. I added the below to my .bashrc file and all is good once again. I'm not sure what the root cause of the issue might be, but this seems to be a good workaround for me.

update_PS1 () {
  PS1="\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$ "
}
shopt -u promptvars
PROMPT_COMMAND=update_PS1
Sign up to request clarification or add additional context in comments.

2 Comments

it's giving me this error: bash: __git_ps1: command not found
For me, make the gitstatus.sh file executable solved the problem. FYI I am using github.com/magicmonty/bash-git-prompt
1

I have the same problem. After I run any git command, the current branch name is disappear. First I use the tip from answer https://stackoverflow.com/a/43659303/8156156. Works well.

But after investigation I find that I have not only this problem but other with bash. I write it in https://github.com/git-for-windows/git/issues/1153#issuecomment-308110681.

I don't know a root of these problems but workaround that works for me (for both problems) is install 32-bit version of Git for Windows (Git-2.13.1-32-bit).

Comments

1

Ubuntu: Show your branch name on your terminal Add these lines in your ~/.bashrc file

# Show git branch name
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

Close and open your terminal

1 Comment

For Windows GitBash you can remove the ${debian_chroot:+($debian_chroot)} as it is not needed. Worked well for me after my branch stopped appearing today after an OS update.
-1

All I had to do is place a blank line at the end of .bashrc, and that solved it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.