Git Bash 提示 - 当前分支名称在 Git 命令后停止显示

Git Bash Prompt- Current Branch Name Stops Showing After Git Command

将 GitBash 与 Git 一起用于 Windows,我的 PS1 变量设置为显示当前分支:

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

开始时效果很好,例如我的提示开始时像

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

但是,在我 运行 任何 git 命令之后,当前分支部分被省略,直到我重新启动 shell。

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

myuser@mypc MINGW64 /c/git/myrepo
$

但是如果我 运行 __git_ps1 它仍然告诉我正确的值:

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

myuser@mypc MINGW64 /c/git/myrepo
$

知道会发生什么,或者我该如何解决 diagnosing/fixing 这个问题?

所以我能够根据 this 和这个问题中已有的信息让事情重新开始。我将以下内容添加到我的 .bashrc 文件中,一切都很好。我不确定问题的根本原因可能是什么,但这对我来说似乎是一个很好的解决方法。

update_PS1 () {
  PS1="\[3]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}[=10=]7\]\n\[3[32m\]\u@\h \[3[35m\]$MSYSTEM \[3[33m\]\w\[3[36m\]`__git_ps1`\[3[0m\]\n$ "
}
shopt -u promptvars
PROMPT_COMMAND=update_PS1

我也遇到了同样的问题。在我 运行 任何 git 命令之后,当前分支名称消失了。首先,我使用答案 中的技巧。效果很好。

但经过调查我发现我不仅有这个问题,还有 bash 的其他问题。我写在https://github.com/git-for-windows/git/issues/1153#issuecomment-308110681.

我不知道这些问题的根源,但对我有用(对于这两个问题)的解决方法是为 Windows 安装 32 位版本的 Git (Git -2.13.1-32 位).

Ubuntu: 在你的终端上显示你的分支名称 在你的 ~/.bashrc 文件中添加这些行

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

关闭并打开您的终端

我所要做的就是在 .bashrc 的末尾放置一个空行,这就解决了。