解决在 mac os 下终端无法显示 git 当前分支的情况

编辑 .bash_profile 文件

1
2
3
4
5
6

vi .bash_profile

# 若不存在,则新建
touch .bash_profile
vi .bash_profile

将下列代码复制粘贴到 .bash_profile 的末尾,保存后重启终端即可。

1
2
3
4
5
6
7
8
9
10
11
12
# Git branch in prompt.
function git_branch {
branch="git branch 2>/dev/null | grep "</span>^*<span class="string">" | sed -e "</span>s/^* //<span class="string">""
if [ "${branch}" != "" ];then
if [ "${branch}" = "(no branch)" ];then
branch="(git rev-parse --short HEAD...)"
fi
echo " ($branch)"
fi
}

export PS1='[email protected] [ 33[01;36m]W[ 33[01;32m]$(git_branch)[ 33[00m] $ '

显示效果
分支显示