centos ssh终端下高亮显示git分支名

ssh终端显示git分支名:

 1 #set git branch
 2 green=$'e[0;32m'
 3 magenta=$'e[0;36m'
 4 normal_colours=$'e[m'
 5 
 6 function find_git_branch {
 7     local dir=. head
 8     until [ "$dir" -ef / ]; do
 9         if [ -f "$dir/.git/HEAD" ]; then
10             head=$(< "$dir/.git/HEAD")
11             if [[ $head == ref: refs/heads/* ]]; then
12                 git_branch="(${head#*/*/})#"
13             elif [[ $head != '' ]]; then
14                 git_branch='(detached)'
15             else
16                 git_branch='(unknown)'
17             fi
18             return
19         fi
20         dir="../$dir"
21     done
22     git_branch='#'
23 }
24 
25 PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
26 PS1="[$green][u@h W][$magenta]$git_branch[$green]\[$normal_colours] "

将上面这段,追加 /etc/profile 或 ~/.bash_profie 或 ~/.bashrc 任何一个的最后,然后source /etc/profile (或source ~/.bash_profile 或 source ~/.bashrc),让其生效即可。

最终效果:

原文地址:https://www.cnblogs.com/zzming/p/14079305.html