Git 受保护分支回退实践:从强制回滚到新增回退提交

2026-07-13运维6 分钟76 次阅读
AI 摘要salmon-ai

本文详细解析了 Git 受保护分支在回退场景中的处理方式:先尝试将 `hotfix` 直接

前言

在多人协作项目中,线上分支经常会设置为受保护分支,例如 rc、master、main 等。
这些分支通常禁止普通开发人员直接 force push,目的是避免误删历史、覆盖他人提交,保障发布链路安全。

这次遇到的问题是:

  • ms-data 服务后续提交中引入了错误代码
  • 希望把代码恢复到一个稳定提交节点
  • 稳定提交为:
bash 复制代码
7972c689878131fa0f13c54e25c7a3a78ca49c69

一开始我们希望“直接回到这个版本,后面的提交都不要了”。
但在实际操作中发现,rc 是 GitLab 保护分支,无法直接强制推送覆盖历史。

最终采用的方案是:

在 hotfix 分支上基于当前 rc 最新代码,新增一条“回退提交”,让代码内容恢复到目标版本,再通过 MR 合并到 rc。

这样既能满足“代码恢复到稳定版本”的目标,也能符合 GitLab 保护分支的权限规则。


一、问题背景

当前目标是将 ms-data 服务恢复到稳定提交:

bash 复制代码
7972c689878131fa0f13c54e25c7a3a78ca49c69

这个提交本身是一个合并节点:

bash 复制代码
7972c6898 Merge branch 'hotfix' into 'rc'

后续 hotfix、rc 又继续产生了一些提交,其中部分代码存在问题,需要回退。

一开始比较直观的想法是:

bash 复制代码
git reset --hard 7972c689878131fa0f13c54e25c7a3a78ca49c69
git push --force-with-lease origin 分支名

这种方式会直接把分支指针移动回指定提交,并覆盖远端分支历史。

如果目标分支不是保护分支,这种方式是可行的。
但如果目标分支是保护分支,就会被 GitLab 拦截。


二、第一次处理:直接回退 hotfix

先确认目标提交存在:

bash 复制代码
git show --no-patch --oneline --decorate 7972c689878131fa0f13c54e25c7a3a78ca49c69

输出类似:

bash 复制代码
7972c6898 Merge branch 'hotfix' into 'rc'

然后将 hotfix 回退到该提交:

bash 复制代码
git switch hotfix
git reset --hard 7972c689878131fa0f13c54e25c7a3a78ca49c69
git push --force-with-lease origin hotfix

这一步执行后,远端 hotfix 确实回到了目标提交:

bash 复制代码
git ls-remote origin refs/heads/hotfix

结果为:

bash 复制代码
7972c689878131fa0f13c54e25c7a3a78ca49c69 refs/heads/hotfix

到这里,hotfix 分支本身已经恢复到了目标版本。


三、为什么 hotfix 合并到 rc 没有变更记录

随后创建 hotfix -> rc 的 MR,发现 GitLab 显示没有变更记录。

这是因为 rc 已经包含了 hotfix 当前这个提交节点。

可以用下面的命令确认:

bash 复制代码
git fetch origin hotfix rc
git merge-base origin/hotfix origin/rc

输出为:

bash 复制代码
7972c689878131fa0f13c54e25c7a3a78ca49c69

这说明:

  • origin/hotfix 当前就是 7972c689...
  • origin/rc 的历史里已经包含了 7972c689...
  • 从 Git 的角度看,hotfix 没有任何新的提交可以合并到 rc

所以 GitLab MR 显示“没有变更”是正常的。

可以进一步查看两个分支的关系:

bash 复制代码
git rev-parse origin/hotfix
git rev-parse origin/rc

结果类似:

bash 复制代码
7972c689878131fa0f13c54e25c7a3a78ca49c69
8215c95cdb05ad8bb654c35a735ca1925b836644

说明 rc 已经走到了更后面的提交:

bash 复制代码
8215c95cdb05ad8bb654c35a735ca1925b836644

此时从 hotfix 合并到 rc,自然没有新内容。


四、尝试直接回退 rc,但被保护分支拦截

既然 rc 才是后续错误代码所在的分支,那么一个直接想法是:
把 rc 也强制回退到 7972c689...。

命令如下:

bash 复制代码
git checkout rc
git fetch origin
git reset --hard 7972c689878131fa0f13c54e25c7a3a78ca49c69
git push --force-with-lease origin rc

但推送时报错:

bash 复制代码
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To https://git.gdyhsp.com/shopteam/ms-data.git
 ! [remote rejected] rc -> rc (pre-receive hook declined)
error: failed to push some refs to 'https://git.gdyhsp.com/shopteam/ms-data.git'

原因很明确:

rc 是 GitLab 保护分支,当前账号没有权限 force push。

这类分支通常只有 Maintainer 或 Owner 才能修改保护策略,或者执行强推操作。


五、最终方案:新增一条回退提交

由于无法强推 rc,最终采用了更适合保护分支的方案:

在 hotfix 上基于当前 rc 最新提交,新增一条回退提交,把代码内容恢复到 7972c689...,然后通过 MR 合并到 rc。

这样做的好处是:

  • 不需要强推 rc
  • 不破坏保护分支历史
  • MR 中能看到明确变更记录
  • 合并后 rc 的代码内容会恢复到目标版本
  • 错误提交仍保留在历史中,但被新的回退提交覆盖掉代码效果

六、具体操作命令

1. 拉取最新远端分支

bash 复制代码
git fetch origin hotfix rc

2. 切换到 hotfix

bash 复制代码
git switch hotfix

3. 让 hotfix 基于当前 rc 最新节点

bash 复制代码
git reset --hard origin/rc

这一步很关键。

它的目的不是最终回退代码,而是让 hotfix 当前 HEAD 先站到 rc 最新提交上。
这样后面生成的回退提交,才是针对当前 rc 的差异。

当前 rc 最新提交类似:

bash 复制代码
8215c95cd Merge branch 'hotfix' into 'rc'

4. 将工作区恢复到目标提交内容

bash 复制代码
git restore --source=7972c689878131fa0f13c54e25c7a3a78ca49c69 --staged --worktree .

这条命令的含义是:

  • 以 7972c689... 这个提交的文件树为准
  • 同时恢复工作区和暂存区
  • 生成一批“从当前 rc 回退到目标版本”的差异

5. 查看变更

bash 复制代码
git status --short --branch

查看文件统计:

bash 复制代码
git diff --stat --cached

本次统计结果显示大约有 26 个文件变更:

bash 复制代码
26 files changed, 1857 insertions(+), 897 deletions(-)

其中包含 Java 文件、SQL 文件以及部分删除文件。

6. 提交回退记录

bash 复制代码
git commit -m "回退ms-data到7972c689版本"

生成的新提交为:

bash 复制代码
9a16284b5 回退ms-data到7972c689版本

7. 推送 hotfix

bash 复制代码
git push origin hotfix

推送成功后,GitLab 返回 MR 地址:

text 复制代码
https://git.gdyhsp.com/shopteam/ms-data/-/merge_requests/3630

七、验证 MR 是否有变更记录

推送后,再次验证 hotfix -> rc 是否存在差异:

bash 复制代码
git fetch origin hotfix rc
git diff --stat origin/rc...origin/hotfix

如果能看到文件变更统计,就说明 MR 中会显示变更记录。

本次验证中,确实看到了 26 个文件的差异,说明处理成功。

也可以查看最新提交:

bash 复制代码
git log --oneline --decorate -4 hotfix

结果类似:

bash 复制代码
9a16284b5 (HEAD -> hotfix, origin/hotfix) 回退ms-data到7972c689版本
8215c95cd (origin/rc) Merge branch 'hotfix' into 'rc'
93151237b Merge branch 'fix/fix-financial-split' into 'hotfix'
58e0fa1d0 rlb-分账-门店对账单-接龙业务

这说明:

  • hotfix 最新提交是回退提交
  • 回退提交的父节点是当前 rc
  • 因此 hotfix -> rc 会产生真实差异

八、几个容易踩坑的点

1. force push 回退分支后,本地不会自动同步

如果远端分支被强制回退,其他人本地执行普通 git pull 不一定会自动丢弃本地后续提交。

需要执行:

bash 复制代码
git fetch origin
git checkout hotfix
git reset --hard origin/hotfix

如果本地有未提交配置,先保存:

bash 复制代码
git stash push -m "保存本地配置"

2. hotfix 回退后,MR 到 rc 可能没有变更

如果 rc 已经包含 hotfix 当前提交,那么 hotfix -> rc 就没有差异。

判断方式:

bash 复制代码
git merge-base origin/hotfix origin/rc

如果输出结果等于 origin/hotfix,说明 hotfix 已经被 rc 包含。

3. 保护分支不能强推

遇到下面错误:

bash 复制代码
GitLab: You are not allowed to force push code to a protected branch

说明目标分支是保护分支,当前账号没有强推权限。

这时不要继续尝试强推,应该改走 MR 或找管理员处理。

4. 新增回退提交和强制回退不是一回事

强制回退:

bash 复制代码
git reset --hard 目标提交
git push --force-with-lease origin 分支

特点:

  • 会改写分支历史
  • 后续提交从该分支记录中消失
  • 需要 force push 权限

新增回退提交:

bash 复制代码
git restore --source=目标提交 --staged --worktree .
git commit -m "回退到目标版本"
git push origin 分支

特点:

  • 不改写历史
  • 会新增一条提交
  • 适合保护分支
  • MR 有明确变更记录

九、推荐流程

如果目标分支不是保护分支,并且确认可以覆盖历史:

bash 复制代码
git fetch origin
git switch 目标分支
git reset --hard 目标提交
git push --force-with-lease origin 目标分支

如果目标分支是保护分支,推荐走新增回退提交:

bash 复制代码
git fetch origin hotfix rc
git switch hotfix
git reset --hard origin/rc
git restore --source=目标提交 --staged --worktree .
git status --short --branch
git diff --stat --cached
git commit -m "回退服务到目标版本"
git push origin hotfix

然后创建或更新 hotfix -> rc 的 MR。


十、本次最终结果

本次最终采用新增回退提交方案。

目标恢复版本:

bash 复制代码
7972c689878131fa0f13c54e25c7a3a78ca49c69

新增回退提交:

bash 复制代码
9a16284b5 回退ms-data到7972c689版本

MR 地址:

text 复制代码
https://git.gdyhsp.com/shopteam/ms-data/-/merge_requests/3630

验证结果:

bash 复制代码
git diff --stat origin/rc...origin/hotfix

确认存在 26 个文件变更,MR 中可以正常看到变更记录。


总结

这次问题的核心并不是 GitLab 没有识别变更,而是分支关系导致 hotfix -> rc 没有新差异:

  • hotfix 已经回退到 7972c689...
  • rc 已经包含了这个节点,并且继续向后提交
  • 所以直接用 hotfix 合并到 rc 不会产生变更

由于 rc 是保护分支,无法通过 force push 直接覆盖历史。
最终通过“基于当前 rc 新增一条回退提交”的方式解决:

bash 复制代码
git reset --hard origin/rc
git restore --source=7972c689878131fa0f13c54e25c7a3a78ca49c69 --staged --worktree .
git commit -m "回退ms-data到7972c689版本"
git push origin hotfix

这是一种更适合团队协作和保护分支规则的处理方式:
既保留了历史记录,又能通过 MR 明确看到代码回退内容,最终让 rc 的代码恢复到目标稳定版本。

觉得有用就点个赞,不用登录也能读

评论

0 条
登录 之后才能评论
—
还没有评论,来说第一句