Display Git Branch Name in Bash

First

将脚本下载到 Home 目录

1
2
$ git clone https://github.com/KipJiang/git-aware-prompt.git ~/git-aware-prompt
$ mv ~/git-aware-prompt ~/.git-aware-prompt

修改配置文件 .bashrc

1
$ vim ~/.bashrc

添加如下内容

1
2
3
4
# source .git-aware-prompt
export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "
export GITAWAREPROMPT=~/.git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"

使配置生效

1
$ source ~/.bashrc

效果

ubuntu-git-color

centos-git–color

Second

编辑 ~/.bashrc,添加如下内容

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

export PS1='\u@\h \[\033[01;36m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\] \$ '

使配置生效

1
$ source ~/.bashrc

Third

编辑 ~/.profile,添加如下内容

1
2
3
4
5
# git branch function
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/ *\(.*\)/(\1)/'
}
export PS1="\u@\h:\[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

使配置生效

1
$ source ~/.profile
© 2019 - 2021 · Kip Jiang · Theme Simpleness Powered by Hugo ·