在 Bash 终端中为 Git 仓库目录添加提示信息
1. 下载 git-prompt.sh 脚本
Git 仓库地址:https://github.com/git/git
从 Git 官方仓库下载 contrib/completion/git-prompt.sh
脚本
wget https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh
# 或者
curl -O https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh
移动 git-prompt.sh
到 /usr/local/share/git/
目录
mkdir -p /usr/local/share/git/
mv ./git-prompt.sh /usr/local/share/git/
2. 运行 git-prompt.sh 脚本
source /usr/local/share/git/git-prompt.sh
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ " "$(tput setaf 2)(%s)$(tput sgr0)"'
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ " "\[\e[0;32m\](%s)\[\e[0m\]"'
将运行命令添加到 ~/.bashrc
文件中
cat <<'EOF' >> ~/.bashrc
source /usr/local/share/git/git-prompt.sh
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ " "$(tput setaf 2)(%s)$(tput sgr0)"'
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ " "\[\e[0;32m\](%s)\[\e[0m\]"'
EOF
此时进入 Git 仓库目录即可在终端看到分支提示
root@localhost:/data/git-test(master)# git checkout -b dev
Switched to a new branch 'dev'
root@localhost:/data/git-test(dev)#