如何更新提示符以显示 Linux 上当前的 git 分支?

Aya*_*abi 2 linux git bash github

我正在开发一个 bash 脚本,它将当前分支添加到我的终端提示符中,并在我 cd 进入终端中 git 存储库的文件夹时显示有关此文件夹中最新提交的信息

问题是,每当我git checkout在该存储库文件夹中切换分支时,提示不会更新当前分支,这是我的.bashrc文件中的 bash 代码

cd() {
  builtin cd "$@"
  local status=$?
  [ $status -eq 0 ] && PS1="[\e[0;32m${debian_chroot:+($debian_chroot)}\w\e[m]\e[0;35m$(parse_git_branch)\e[m \n$ "
  if [ -d .git ]; then
        echo -e "\nMost Recent Commit"
        git show --summary;

  fi
  return $status
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*ely 5

正如Pro Git 书中所述,您需要该git-prompt.sh文件(应作为 Git 的一部分安装),然后在您中.bashrc执行以下操作:

. /usr/share/git-core/contrib/completion/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\w$(__git_ps1 " (%s)")\$ '
Run Code Online (Sandbox Code Playgroud)