gitbash命令行前缀个性化设置
- 先看最终显示效果
[magiccube.git@Elvis:magiccube](develop)$
##显示的字段解释:
[{git库名称}@{git.congfig.user.name}:{当前文件夹目录名称}({分支名称})]
设置步骤
- 编辑.bashrc文件
vim ~/.bashrc
- 在该文件中添加如下代码
function git_branch {
branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
if [ "${branch}" != "" ];then
if [ "${branch}" = "(no branch)" ];then
branch="(`git rev-parse --short HEAD`...)"
fi
echo "($branch)"
fi
}
#显示目录
function get_pwd {
# echo `pwd`;
#获取远程代码库名称
git_url="`git remote get-url origin 2>/dev/null`"
if [ "${git_url}" != "" ];then
git_url="${git_url##*/}"
fi
project_path=$(basename `pwd`)
username=`git config --global user.name 2>/dev/null`
if [ "${username}" != "" ];then
username="@${username}:"
fi
echo "${git_url}${username}${project_path}"
}
export PS1='[$(get_pwd)]\033[01;36m$(git_branch)\[\033[00m\]\$ '
- 显示的颜色可以自行设置
01;36m部分的36是可以替换设置其他颜色值的:
可选颜色:红色、绿色、黄色、蓝色、洋红、青色和白色。 他们对应的颜色代码是:30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
参考资料:https://blog.csdn.net/sayhello_world/article/details/85237564