【git】删除本地以及远端已经合并到master的分支
系统windows终端ConEmu需求很多本地以及远端存储了已经合并到了master的分支需要统一清理清理分支本地和远端已经合并到master的分支保护分支本地和远端的master分支以master开头以release-开头的分支,以及 dev test sit uat main 这5个分支以及以他们开头的本地和远端的都要清理需要二次确认脚本内容注意上面的操作系统# 清理已合并的远端分支# 清理已合并分支的函数# 功能删除远端和本地已合并到 master 的分支 (排除 release-*)# 清理已合并分支的函数# 修复了 grep 参数报错问题优化了分支过滤逻辑clean-merged-branches(){echo 开始检查已合并到 master 的分支...# 1. 确保获取了最新的远端信息echoecho 同步远端仓库信息...gitfetch--prune--allgitcheckout master2/dev/null||echo已在 master 分支gitpull origin master# # 第一步处理远端分支# echoecho 正在查找远端已合并的分支...localremote_to_delete()localremote_to_skip()# 逐行读取远端已合并的分支whileIFSread-rline;do# 去除开头的空格line$(echo$line|seds/^[ ]*//)# 跳过包含 - 的行HEAD 指向if[[$line*-*]];thencontinuefi# 去除 origin/ 前缀branch${line#origin/}# 跳过空行if[[-z$branch]];thencontinuefi# --- 核心过滤逻辑 ---# 排除 master、master-*、release-*、test、test-*、uat、uat-*、main、main-*、sit、sit-*、dev、dev-* 开头的分支if[[$branchmaster]];thenremote_to_skip($branch(主分支))elif[[$branch~^master-]];thenremote_to_skip($branch(master前缀分支))elif[[$branch~^release-]];thenremote_to_skip($branch(release分支))elif[[$branch~^test]];thenremote_to_skip($branch(test分支))elif[[$branch~^uat]];thenremote_to_skip($branch(uat分支))elif[[$branch~^main]];thenremote_to_skip($branch(main分支))elif[[$branch~^sit]];thenremote_to_skip($branch(sit分支))elif[[$branch~^dev]];thenremote_to_skip($branch(dev分支))elseremote_to_delete($branch)fidone(gitbranch-r--mergedorigin/master)# 显示被跳过的分支调试用可选if[${#remote_to_skip[]}-gt0];thenechoecho⏭️ 以下分支被自动跳过受保护forbrin${remote_to_skip[]};doecho -$brdonefiif[${#remote_to_delete[]}-eq0];thenechoecho✅ 没有发现需要清理的远端分支。elseechoecho 发现以下远端分支已合并到 masterforbrin${remote_to_delete[]};doecho - origin/$brdoneechoread-p⚠️ 确认删除上述远端分支吗(y/N): confirmif[[$confirm~^[Yy]$]];thenif[${#remote_to_delete[]}-gt0];thengitpush origin--delete${remote_to_delete[]}echo✅ 远端分支删除完成。fielseecho❌ 取消删除远端分支。fifi# # 第二步处理本地分支# echoecho 正在查找本地已合并的分支...locallocal_to_delete()locallocal_to_skip()# 逐行读取本地已合并的分支whileIFSread-rline;do# 去除开头的空格和 * 标记branch$(echo$line|seds/^[* ]*//)# 跳过空行if[[-z$branch]];thencontinuefi# --- 核心过滤逻辑 ---# 排除 master、master-*、release-*、test、test-*、uat、uat-*、main、main-*、sit、sit-*、dev、dev-* 开头的分支if[[$branchmaster]];thenlocal_to_skip($branch(主分支))elif[[$branch~^master-]];thenlocal_to_skip($branch(master前缀分支))elif[[$branch~^release-]];thenlocal_to_skip($branch(release分支))elif[[$branch~^test]];thenlocal_to_skip($branch(test分支))elif[[$branch~^uat]];thenlocal_to_skip($branch(uat分支))elif[[$branch~^main]];thenlocal_to_skip($branch(main分支))elif[[$branch~^sit]];thenlocal_to_skip($branch(sit分支))elif[[$branch~^dev]];thenlocal_to_skip($branch(dev分支))elselocal_to_delete($branch)fidone(gitbranch--mergedmaster)# 显示被跳过的分支调试用可选if[${#local_to_skip[]}-gt0];thenechoecho⏭️ 以下本地分支被自动跳过受保护forbrin${local_to_skip[]};doecho -$brdonefiif[${#local_to_delete[]}-eq0];thenechoecho✅ 没有发现需要清理的本地分支。elseechoecho 发现以下本地分支已合并到 masterforbrin${local_to_delete[]};doecho -$brdoneechoread-p⚠️ 确认删除上述本地分支吗(y/N): confirm_localif[[$confirm_local~^[Yy]$]];thenif[${#local_to_delete[]}-gt0];thengitbranch-d${local_to_delete[]}echo✅ 本地分支删除完成。fielseecho❌ 取消删除本地分支。fifiechoecho 清理任务结束。}直接把这个内容放到 ~/.bashrc文件里面windows打开方式notepad ~/.bashrc然后source~/bashrc然后重启 conemu找到你的git仓库执行clean-merged-branches