目录
正在加载目录…

删除已经合并的 Git 远程分支

发布于 2026-11-05 21:35 · 最后编辑于 2026-11-05 21:35 · 版本 v2 · 字数 980 · 预计阅读 2 分钟 👁 11 次阅读

清理远程分支前,先回答两个问题:该分支的最新提交是否已经进入目标分支?确认之后,远程分支有没有又被推送?这里用一个默认只检查、每次只处理一个分支的脚本,把两个判断分开。

TL;DR

用 git merge-base --is-ancestor 判断提交可达性。真正删除时,用刚检查过的提交 ID 限制删除条件,远程分支变化就让 Git 拒绝操作。

先预览,再决定是否删除

在普通 Git 工作仓库中运行,需要能读取指定远程。下面把目标分支写成 main,把候选写成 topic/report;改成仓库里确实存在的名称。脚本默认只读取远程并抓取提交对象,不删除远程分支。

保存为 clean-merged-branch.sh:

bash
#!/usr/bin/env bash
fail() { printf '%s\n' "$*" >&2; exit 1; }
apply=0
if [[ ${1:-} == --apply ]]; then
    apply=1
    shift
fi
if (( $# != 3 )); then
    printf 'Usage: %s [--apply] REMOTE BASE BRANCH\n' "${0##*/}" >&2
    exit 2
fi
remote=$1 base_ref=refs/heads/$2 branch_ref=refs/heads/$3
[[ $remote =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || fail '远程名称不符合脚本约定'
git check-ref-format "$base_ref" && git check-ref-format "$branch_ref" ||
    fail '目标或候选分支名无效'
[[ $branch_ref != "$base_ref" ]] || fail '不能删除目标分支'

# 拒绝取数与写入端点不一致,以及多推送地址。
fetch_url=$(git remote get-url --all "$remote") || exit 1
push_url=$(git remote get-url --push --all "$remote") || exit 1
[[ -n $fetch_url && $fetch_url != *$'\n'* && $fetch_url == "$push_url" ]] ||
    fail '脚本要求唯一且相同的 fetch/push 地址'

git fetch --no-tags "$remote" "$base_ref" "$branch_ref" || exit 1
snapshot=$(git ls-remote --symref "$remote" HEAD "$base_ref" "$branch_ref") || exit 1
default_ref= base_oid= branch_oid=
while IFS=$'\t' read -r value ref; do
    if [[ $ref == HEAD && $value == 'ref: refs/heads/'* ]]; then
        default_ref=${value#ref: }
    elif [[ $ref == "$base_ref" ]]; then
        base_oid=$value
    elif [[ $ref == "$branch_ref" ]]; then
        branch_oid=$value
    fi
done <<< "$snapshot"
[[ -n $default_ref && $branch_ref != "$default_ref" ]] ||
    fail '默认分支未知,或候选就是默认分支'
oid_pattern='^([0-9a-f]{40}|[0-9a-f]{64})$'
[[ $base_oid =~ $oid_pattern && $branch_oid =~ $oid_pattern ]] ||
    fail '远程快照缺少有效分支提交'
if git merge-base --is-ancestor "$branch_oid" "$base_oid"; then
    printf '已合入:%s @ %s\n' "$branch_ref" "$branch_oid"
else
    code=$?
    if (( code == 1 )); then
        fail '候选提交不是目标提交的祖先'
    fi
    fail '无法比较提交;检查浅克隆或抓取期间远程是否已变化'
fi

if (( ! apply )); then
    printf '仅预览;核对仓库规则后用 --apply 重新检查并删除。\n'
    exit 0
fi
# 空来源的 refspec 删除精确的远程 ref;lease 要求它仍指向已检查的提交。
git -c push.followTags=false push --recurse-submodules=no \
    --force-with-lease="$branch_ref:$branch_oid" "$remote" ":$branch_ref" || exit 1
printf '已删除:%s\n' "$branch_ref"

先运行 bash clean-merged-branch.sh origin main topic/report。若输出“已合入”和“仅预览”,候选仍应出现在 git ls-remote origin refs/heads/topic/report 的输出中。确认候选名称和团队的分支策略后,才运行 bash clean-merged-branch.sh --apply origin main topic/report。预览会执行 git fetch,可能写入本地对象、引用和 FETCH_HEAD,但不会推送或删除远端分支。删除成功后,用 git ls-remote --exit-code origin refs/heads/topic/report 再查:状态 2 表示没有匹配;认证或连接失败不能当作删除成功。

判断的依据与限制

祖先判断回答的是“这些提交已经可以从目标分支追溯到吗”。压缩合并或变基可能保留改动却生成不同的提交,因此脚本会保守地拒绝这类候选。不要用分支名的子串匹配替代提交判断。浅克隆缺少历史时也可能得不到肯定结论,先取得足够历史再判断。

删除条件只约束候选分支的提交 ID。它能避免覆盖候选的新推送,但无法在客户端同时锁住远程默认分支、目标分支和分支保护规则。对允许并发改写目标历史的仓库,应交给服务端的合并与清理机制。脚本的状态判断与参数处理对应 条件、循环与函数:组织自动化脚本。

参考资料

← 返回列表

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论