Linux下使用git设置显示分支名称和改变颜色
Linux下使用git设置显示分支名称和改变颜色
设置后的样例
废话不多说,先展示设置后的样例给你们看看。效果还是可以的吧!!!
分步教程
1、找到要修改的目标文件
首先找到要修改的目标文件,文件名为“.bashrc”,我的文件路径是~/.bashrc
,即进入/home/用户名
路径后用ls -a
即可以看到该文件。
2、修改目标文件
对该文件进行编辑gedit ./.bashrc
或者使用其他编辑器例如vim也可以。添加如下代码:
## Parses out the branch name from .git/HEAD:
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[email protected]$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"
export PS1
3、成功啦
保存退出,执行source ~/.bashrc
或者. ~/.bashrc
即可发生变化。
PS:如果有人知道这个文件中的内容用的是什么语法,希望能给我评论告知。