方式一:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
function git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
echo "<"${ref#refs/heads/}"> ";
}
function parse_git_dirty {
local git_status=$(git status 2> /dev/null | tail -n1) || $(git status 2> /dev/null | head -n 2 | tail -n1);
if [[ "$git_status" != "" ]]; then
local git_now; # 标识
if [[ "$git_status" =~ nothing\ to\ commit || "$git_status" =~ Your\ branch\ is\ up\-to\-date\ with ]]; then
git_now="=";
elif [[ "$git_status" =~ Changes\ not\ staged || "$git_status" =~ no\ changes\ added ]]; then
git_now='~';
elif [[ "$git_status" =~ Changes\ to\ be\ committed ]]; then #Changes to be committed
git_now='*';
elif [[ "$git_status" =~ Untracked\ files ]]; then
git_now="\+";
elif [[ "$git_status" =~ Your\ branch\ is\ ahead ]]; then
git_now="#";
fi
echo "${git_now}";
fi
}
PS1="[\[\e[1;36m\]\u\[\e[1;32m\]\w\[\e[0m\]] \[\e[0m\]\[\e[1;35m\]\$(git_branch)\[\033[0;31m\]\$(parse_git_dirty)\[\033[0m\]]\$ "
#PS1="[\[\e[1;38m\]\u\[\e[1;38m\]\w\[\e[0m\]] \[\e[0m\]\[\e[1;36m\]\$(git_branch)\[\033[0;31m\]\$(parse_git_dirty)\[\033[0m\]]\$ "
方式二:
find_git_branch () {
local dir=. head
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
head=$(< "$dir/.git/HEAD")
if [[ $head = ref:\ refs/heads/* ]]; then
git_branch=" → ${head#*/*/}"
elif [[ $head != '' ]]; then
git_branch=" → (detached)"
else
git_branch=" → (unknow)"
fi
return
fi
dir="../$dir"
done
git_branch=''
}
PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
# Heree
black=$'\[\e[1;30m\]'
red=$'\[\e[1;31m\]'
green=$'\[\e[1;32m\]'
yellow=$'\[\e[1;33m\]'
blue=$'\[\e[1;34m\]'
magenta=$'\[\e[1;35m\]'
cyan=$'\[\e[1;36m\]'
white=$'\[\e[1;37m\]'
normal=$'\[\e[m\]'
#PS1="$white[$magenta\u$white@$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"
PS1="$yellow\u$white:$cyan\w$magenta\$git_branch\$ $normal"