1.在 .bash_profile 中添加如下代码:
function git-branch-name {
git symbolic-ref --short -q HEAD 2>/dev/null
}
function git-branch-prompt {
local branch=`git-branch-name`
if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "
2.source下 .bash_profile
source ~/.bash_profile
3.效果示例: