本文目录导读:

使用 post-merge 钩子(推荐)
在 .git/hooks/post-merge 中添加:
#!/bin/bash
# post-merge 钩子:合并后自动清理已合并到当前分支的本地分支
# 获取当前分支名称
current_branch=$(git rev-parse --abbrev-ref HEAD)
# 排除保护分支
protected_branches="main|master|develop|release|production"
echo "🔍 开始清理已合并到 '$current_branch' 的分支..."
# 删除已合并到当前分支的本地分支(排除当前分支和保护分支)
merged_branches=$(git branch --merged | grep -v "\*" | grep -v -E "$protected_branches")
if [ -n "$merged_branches" ]; then
echo "$merged_branches" | while read branch; do
branch=$(echo "$branch" | xargs) # 去除空格
echo "🗑️ 删除已合并分支: $branch"
git branch -d "$branch" 2>/dev/null
done
echo "✅ 清理完成"
else
echo "✅ 没有需要清理的已合并分支"
fi
用法:
chmod +x .git/hooks/post-merge
带确认提示的版本
#!/bin/bash
# post-merge 带确认的清理版本
current_branch=$(git rev-parse --abbrev-ref HEAD)
protected_branches="main|master|develop|release|production"
# 获取已合并的分支列表(排除当前和保护分支)
merged_branches=$(git branch --merged | grep -v "\*" | grep -v -E "$protected_branches" | sed 's/^[ \t]*//')
if [ -z "$merged_branches" ]; then
echo "✅ 没有需要清理的已合并分支"
exit 0
fi
echo "📋 以下分支已合并到 '$current_branch':"
echo "$merged_branches"
echo ""
read -p "是否删除这些分支?(y/N): " confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
echo "$merged_branches" | while read branch; do
if [ -n "$branch" ]; then
echo "🗑️ 删除: $branch"
git branch -d "$branch"
fi
done
echo "✅ 清理完成"
else
echo "⏭️ 跳过清理"
fi
检查远程分支是否已删除的版本
#!/bin/bash
# post-merge 检查远程分支是否已删除
current_branch=$(git rev-parse --abbrev-ref HEAD)
protected_branches="main|master|develop|release|production"
echo "🔍 检查已合并分支..."
# 更新远程分支列表
git fetch --prune 2>/dev/null
# 获取本地已合并且远程已删除的分支
for branch in $(git branch --merged | grep -v "\*" | grep -v -E "$protected_branches"); do
branch=$(echo "$branch" | xargs)
# 检查远程是否还有该分支
remote_exist=$(git ls-remote --heads origin "$branch" 2>/dev/null | wc -l)
if [ "$remote_exist" -eq 0 ]; then
echo "🗑️ 删除本地分支(远程已删除): $branch"
git branch -d "$branch" 2>/dev/null
fi
done
echo "✅ 清理完成"
单独的命令行脚本
如果你不想使用钩子,也可以创建一个独立脚本:
~/.git-cleanup.sh:
#!/bin/bash
# 手动清理已合并分支脚本
cleanup_merged_branches() {
local current_branch=$(git rev-parse --abbrev-ref HEAD)
local protected_branches="main|master|develop"
echo "当前分支: $current_branch"
echo "正在查找已合并的分支..."
git branch --merged | grep -v "\*" | grep -v -E "$protected_branches" | xargs -r git branch -d
echo "✅ 清理完成"
}
# 如果你还想清理远程已删除的本地跟踪分支
cleanup_remote_tracking() {
echo "清理远程追踪分支..."
git remote prune origin
}
cleanup_merged_branches
cleanup_remote_tracking
在 .bashrc 或 .zshrc 中添加别名:
alias cleanup-git='sh ~/.git-cleanup.sh'
多分支策略的智能清理
如果你的工作流有多个基础分支(如 develop, staging, production):
#!/bin/bash
# 智能清理:根据当前分支类型清理
current_branch=$(git rev-parse --abbrev-ref HEAD)
# 确定基础分支
if [[ "$current_branch" == "main" || "$current_branch" == "master" ]]; then
base_branch="origin/main"
echo "检测到主分支,检查合并到 origin/main 的分支"
elif [[ "$current_branch" == "develop" ]]; then
base_branch="origin/develop"
echo "检测到开发分支,检查合并到 origin/develop 的分支"
else
base_branch="origin/develop"
echo "功能分支,检查合并到 origin/develop 的分支"
fi
# 获取合并到指定远程分支的本地分支
merged_branches=$(git branch --merged "$base_branch" | grep -v "\*" | grep -v -E "main|master|develop|release|production")
if [ -n "$merged_branches" ]; then
echo "以下分支已合并到 $base_branch:"
echo "$merged_branches"
echo ""
# 排除当前分支
filtered=$(echo "$merged_branches" | grep -v "$current_branch")
if [ -n "$filtered" ]; then
echo "$filtered" | xargs -r git branch -d
fi
fi
安装和设置
-
启用全局钩子目录(可选):
git config --global core.hooksPath ~/.git-hooks mkdir -p ~/.git-hooks
-
或为单个项目设置:
cd your-project chmod +x .git/hooks/post-merge
-
设置保护分支(防止意外删除):
# 在钩子脚本中修改 protected_branches 变量 protected_branches="main|master|develop|release|production|v[0-9]+"
注意事项
- 安全性:始终保留
-d(安全删除),而不是-D(强制删除) - 保护分支:永远保护 main/master/develop 等关键分支
- 测试:先在测试仓库中测试脚本
- 配合使用:可以结合
post-checkout检查,在切换分支时也清理
这些脚本可以帮助你保持工作区的整洁,避免大量已合并分支堆积,选择合适的版本并根据你的工作流进行调整。