mac bash上显示git分支与状态

 

主要实现

  • 显示当前路径
  • 显示当前所在分支
  • 显示当前修改状态
    • = 表示一个干净的分支
    • ~ 表示文件有改动
    • # 表示已commit 但未 push

 

通过网上搜索和自己根据实际需要修改的代码如下:

.bash_profile

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
32
function parse_git_dirty {
    local git_status=$(git status 2> /dev/null | tail -n1) || $(git status 2> /dev/null | head -n 2 | tail -n1);
    local git_now;
    if [[ "$git_status" != "" ]]; then
        #local git_now; # 标示
    #echo "${git_status}"
        if [[ "$git_status" =~ nothing\ to\ commit || "$git_status" =~  Your\ branch\ is\ up\-to\-date\ with ]]; then
            git_now="=";
        elif [[ "$git_status" =~ no\ changes\ added\ to\ commit ]]; then
            git_now='~';
       # elif [[ "$git_status" =~ Changes\ to\ be\ committed ]]; then #Changes to be committed
        #    git_now='*';
       # elif [[ "$git_status" =~ Untracked\ files ]]; then
        #    git_now="+";
       # elif [[ "$git_status" =~ Your\ branch\ is\ up\-to\-date\ with ]]; then
        #    git_now="#";
        fi
       # echo "${git_now}";
    elif [[ "$git_status" = "" ]]; then
    git_now="#";
    fi
   echo "${git_now}"
}
 
 
function git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
    echo "("${ref#refs/heads/}") ";
}
 
 
PS1="[\[\033[1;32m\]\w\[\033[0m\]] \[\033[0m\]\[\033[1;36m\]\$(git_branch)\[\033[0;31m\]\$(parse_git_dirty)\[\033[0m\]$ "

  

 

posted @   地铁程序员  阅读(1393)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示