ubuntu系统终端显示git分支名

346 阅读1分钟

编辑~/.bashrc文件

1 搜索关键字PS1,将下图中位置:

image.png

修改为如下代码:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\033[31m$(git_branch)\033[0m\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\033[31m$(git_branch)\033[0m\$ '
fi
unset color_prompt force_color_prompt

2 在文件最后添加:

git_branch()
{
   branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`
   if [ "${branch}" != "" ]
   then
       if [ "${branch}" = "(no branch)" ]
       then
           branch="(`git rev-parse --short HEAD`...)"
       fi
       echo "($branch)"
   fi
}

3 效果:

image.png