王大拿
知道的越多,不知道的也就越多! 只要学不死,就往死里学!!!
随笔 - 118,  文章 - 1,  评论 - 7,  阅读 - 82255

找到要修改的目标文件

首先找到要修改的目标文件,文件名为“.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

效果展示

image
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
posted on   DevOps_SRE  阅读(243)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示