Bash 命令行显示 Git 分支名称

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shida_csdn/article/details/87933135

只需在 ~/.bashrc 最后追加如下内容:

function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s] " $branch; fi
}
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w$(git-branch-prompt)\$ '

注意:PS1 的内容使用单引号引起来,而非双引号

          很多使用双引号的错误教程,误导大家,不要上当!

执行 source 命令立即生效

source ~/.bashrc

 然后,随便 cd 进入一个 git 仓库就能看到效果了,效果如下:

猜你喜欢

转载自blog.csdn.net/shida_csdn/article/details/87933135