「Git」- -bash: __git_ps1: command not found

问题描述

在 Bash 中,可以将 __git_ps1 添加到 PS1 中,已在命令行显示当前分支。

但是,在 CentOS 7.4 中,设置 export PS1="\u@\h \w "'$(__git_ps1 " (%s)")'"# " 之后,产生如下错误:

-bash: __git_ps1: command not found

问题原因

原因有很多,这里只记录我们遇到的场景:
1)这个 Shell 函数是 git 提供的(不是 1Bash Completion 提供),而 git-prompt.sh 没有载入。

解决办法

第一步、下载脚本

curl -o /etc/bash_completion.d/git-prompt.sh \
    https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

第二步、重新登录,或者 source /etc/bash_completion.d/git-prompt.sh

参考文献

macos – (Mac) -bash: __git_ps1: command not found – Stack Overflow