找到要修改的目标文件
首先找到要修改的目标文件,文件名为“.bashrc”,默认文件路径是~/.bashrc
修改目标文件
vim ~/.bashrc
文件的末尾追加以下代码
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"
# Here is bash color codes you can use
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,这里的“\u表示root,\h表示host,颜色什么的完全可以自定义”
PS1="$white[$magenta\u$white@$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"
export PS1
保存退出,刷新配置
source ~/.bashrc
或者
~/.bashrc
效果展示
centos 和redhat 下添加如下配置更加完美
[root@RedHat6:~]$ git config --global color.status auto
[root@RedHat6:~]$ git config --global color.diff auto
[root@RedHat6:~]$ git config --global color.branch auto
[root@RedHat6:~]$ git config --global color.interactive auto