删除与找回 Git 提交:reset、rebase 和 reflog

February 19, 2025

从分支中移除一个提交,和从仓库中彻底抹去这个提交,是两种操作。这解释了为什么 reset 后 git log 里不见的提交,仍可能通过 git reflog 找回来。

这篇笔记起于一份删除提交的指南,以及一个关于强制推送后如何找回提交的问题。把两者放在一起,可以理出一套流程:确定是撤销改动还是重写历史,保留恢复点,并弄清操作之后本地还留有哪些记录。

先确定想移除什么

目标方法
抵消一个共享提交的改动用 git revert 创建反向提交。
从本地分支移除最后一次提交用 git reset,并选择如何处理改动。
排除分支中间的一次提交交互式 rebase 时使用 drop。
恢复先前的分支顶端在 reflog 里定位,再创建恢复分支。
移除分支名删除分支引用,其提交仍可能从别处可达。
保留当前快照并重新开始历史高级历史重写:创建一个新的根提交。

改动历史之前,先查看当前检出状态:

git status
git log --oneline --graph --decorate -20

新建分支,保留当前已提交的历史:

git branch backup-before-rewrite

选择未被占用的备份分支名。它保留当前提交及其祖先,未提交的文件改动要在破坏性操作前另外保存。以下示例假设工作树干净,要编辑的分支叫 topic。

撤销共享提交的改动并保留历史

如果别人已经在使用这个分支,最简单的方法往往是新建提交来反转之前的更改:

git revert COMMIT_ID

把 COMMIT_ID 换成要撤销的提交。原提交留在历史里,新提交记录反向改动。后来发生的更改可能导致冲突。撤销合并提交需要选定主线父提交,并理解这对未来合并的影响,不能照搬普通单父提交的做法。1

用 git reset 撤销最后一次提交并保留改动

要撤销最后一次本地提交并保留改动,可以用 git reset --soft HEAD~1 让改动留在暂存区,或用 git reset HEAD~1 让它们留在工作树但取消暂存。有父提交时,几种 reset 模式的区别如下。2

命令结果
git reset --soft HEAD~1分支退回一次提交,索引和工作树保持原样。
git reset HEAD~1分支退回并重置索引,工作树改动保留为未暂存状态。
git reset --hard HEAD~1分支退回,使索引和工作树与目标提交一致。

HEAD~1 沿第一个父提交回溯,无法通用于挑选合并的哪一侧应保留。根提交没有父提交,因此这个表达式无法移除仓库里唯一的一次提交。

需要改写提交但保留暂存状态时用 --soft。想重新选择暂存内容时用默认的 mixed reset。只有连工作树改动也打算丢弃时才用 --hard。妨碍检出目标的未跟踪文件或目录也可能被覆盖或删除。2

reset 后,旧提交可能仍可通过其他引用或 reflog 找到。移动分支指针不会立刻删除对象。

用交互式 rebase 移除中间的一次提交

对于简单的线性历史,从待移除提交的父提交开始 rebase:

git rebase -i COMMIT_TO_REMOVE^

COMMIT_TO_REMOVE 指的是想移除的提交本身,^ 选取它的父提交。如果先指定前一个提交再加 ^,起点就比预期多退了一次。

把不需要的那一行从 pick 改成 drop,然后保存待办列表。Git 会在选定的基点上重放其余提交。后续提交可能依赖被移除的改动,因此要解决冲突,并检查结果文件。3

# After resolving and staging a conflict:
git rebase --continue

# To abandon an in-progress rebase:
git rebase --abort

如果根提交也要纳入编辑,使用 git rebase -i --root。对于含合并提交的历史,普通 rebase 不会保留原合并结构。--rebase-merges 是另一套需要单独审查的流程。3

用 reflog 找回提交

Git 文档给出了定义:

“Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.”

也就是说,引用日志记录本地仓库中的分支和其他引用在什么时间更新了指向。

— Git, git-reflog, “Description,” para. 2.4

查看 HEAD 或特定分支近期移动的记录:

git reflog
git reflog show topic

查找非预期 reset 或 rebase 之前的提交。复制其提交 ID,避免依赖后续操作可能改变的 reflog 序号。检查候选提交并给它一个分支名:

git show RECOVERED_COMMIT_ID
git branch recovered-work RECOVERED_COMMIT_ID

创建这个分支可以为找回的历史保留引用,同时不移动当前分支,也不改工作文件。然后根据出错以后又做了哪些工作选择恢复方式。5

恢复分支之前的位置

如果找回的提交就是想要的分支顶端,先备份较新的工作,再恢复分支:

git switch topic
git branch backup-before-recovery
git reset --hard recovered-work

如上所述,reset 会替换索引和工作树。指向已有的旧提交,也恢复了访问其祖先历史(包括合并提交)的途径,其他分支引用则不会自动重建。2

保留新工作,重放丢失的单次改动

如果分支上有值得保留的新提交,可以重新应用一个丢失的普通提交:

git switch topic
git cherry-pick RECOVERED_COMMIT_ID

这会将该提交的更改应用到当前历史,通常产生一个新的提交 ID。多个相互依赖的提交需按正确顺序恢复,合并提交需要选择主线父提交。Jonathan.Brink 的原回答同样区分了回到旧位置和在新增工作后用 cherry-pick 恢复。56

恢复有哪些限制?

同一讨论中还有一处重要说明:

“Yes, reflog is stored locally and will be unique to you”

意思是,reflog 保存在本地,每个人的记录都不同。

— Jonathan.Brink (2022, October 4), comment on the recovery answer.7

一个克隆不会继承另一个克隆的 reflog。如果本地从来没有所需提交,就检查可能还保有它的其他克隆或备份。删除分支也会删除该分支的 reflog,不过 HEAD 或其他引用可能仍能帮你找到旧提交。48

reflog 记录会过期。Git 的默认值是普通条目保留 90 天、从当前顶端不可达的条目保留 30 天,具体受配置影响。这是默认保留期限,不能当成保证找得回的截止日。4

没有任何保留的引用或 reflog 条目保护对象后,垃圾回收最终可能删除它。尝试找回工作期间,避免使 reflog 过期或清理对象。reflog 也不会记录任意未保存或未提交的工作树内容。9

重写历史后更新远程分支

重写本地分支不改变远程分支。大家已同意重写历史时,显式指定预期的远端顶端,让并发更新引起推送失败。10

# Before rewriting, fetch and inspect the remote branch.
git fetch origin
git log --oneline --graph --decorate topic origin/topic
expected_tip=$(git rev-parse refs/remotes/origin/topic)

# Perform and review the intended local rewrite, then:
git push --force-with-lease=refs/heads/topic:"$expected_tip" origin topic:topic

在同一个 Bash 会话里记录提交并执行推送。lease 要求远程分支仍指向所记录的提交。它无法证明重写保留了所有预期改动,也不能覆盖服务端保护规则。

普通 --force 没有这项比较。简写的 --force-with-lease 通常依赖远程跟踪引用,后台 fetch 可能更新引用,削弱这个前提。如果这个差别很重要,显式记录并审查预期的远端顶端。10

找回提交后,先看普通推送是否够用。恢复操作本身不要求再强推一次。

删除分支和删除对象的区别

先切换到其他分支,再删除 topic:

git switch main
git branch -d topic

-d 会检查分支是否已经完全合入其上游。没有上游时,就检查是否合入 HEAD。只有确定要跳过检查时才用 git branch -D topic。已在另一个工作树中检出的分支不能这样直接删除。8

删除远程分支是单独的操作:

git push origin --delete topic

通过其他分支或标签可达的提交仍然属于相应历史。失去此类引用的提交可能暂时保留,但不能把它们当作永久备份。reflog 和垃圾回收决定后续是否还有本地恢复机会。89

高级操作:用一个根提交替换分支历史

原草稿包含一条命令,用来舍弃分支的祖先而保留已提交快照。更便于检查的写法是先在独立分支下构建新历史:

new_root=$(git commit-tree 'HEAD^{tree}' -m "Initial snapshot")
git branch replacement-history "$new_root"
git log --oneline replacement-history
git diff HEAD replacement-history

git commit-tree 用当前树创建提交。因为没有用 -p 指定父提交,它是一个根提交。文件快照未变,因此 diff 应为空。未提交文件不在其中。11

检查结果并保存原历史以后,再替换目标分支:

git switch topic
git reset --hard replacement-history

这样得到的分支历史包含一次新提交,并非空历史。其他引用、克隆和备份仍可能留有旧提交。更新已有远程分支仍需要前述协调过的重写流程。

在 GitHub 防止意外重写历史

为共享分支设置删除和强制更新保护。默认情况下,GitHub 的分支保护会对匹配的分支禁用强推和删除。检查规则适用范围,以及允许哪些主体绕过。12

原笔记还描述了一项企业级策略。以下步骤专用于 GitHub Enterprise Server 3.12,普通 GitHub.com 仓库的设置页面不同:

  1. 打开 Enterprise settings → Policies → Options。
  2. 在 Force pushes 下,选择允许强推、处处禁止强推,或仅禁止默认分支强推。
  3. 如果策略必须覆盖组织与仓库设置,选择 Enforce on all repositories。13

策略由企业向下作用于所有者和仓库。实际应用时,要使用与已部署服务器版本相匹配的文档。

删除历史无法保证秘密已经清除

原草稿收录的指南明说了这一点:

“Once sensitive data has been committed to your repo you should immediately consider it compromised.”

意思是,敏感数据一旦提交到仓库,就应立即视为已泄露。

— Foster, How to delete a commit from Git history, introductory note.14

凭据已经暴露时,先撤销或轮换,再清理历史。重写分支无法收回协作者已经获取的副本,也不能移除所有服务端引用。对于这项独立任务,应按照托管服务提供商专门的敏感数据移除流程处理。15

参考资料

Footnotes

  1. Git. (n.d.). git-revert. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-revert ↩

  2. Git. (n.d.). git-reset. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-reset ↩ ↩2 ↩3

  3. Git. (n.d.). git-rebase. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-rebase ↩ ↩2

  4. Git. (n.d.). git-reflog. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-reflog ↩ ↩2 ↩3

  5. Jonathan.Brink. (2016, January 12). [关于用 reflog、reset 和 cherry-pick 恢复提交的回答]. Stack Overflow. https://stackoverflow.com/a/34751908 ↩ ↩2

  6. Git. (n.d.). git-cherry-pick. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-cherry-pick ↩

  7. Jonathan.Brink. (2022, October 4). [说明 reflog 保存在各自本地仓库的评论]. Stack Overflow, 恢复回答下的评论. https://stackoverflow.com/a/34751908 ↩

  8. Git. (n.d.). git-branch. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-branch ↩ ↩2 ↩3

  9. Git. (n.d.). git-gc. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-gc ↩ ↩2

  10. Git. (n.d.). git-push. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-push ↩ ↩2

  11. Git. (n.d.). git-commit-tree. 检索于 2025 年 2 月 19 日,来源: https://git-scm.com/docs/git-commit-tree ↩

  12. GitHub. (n.d.). About protected branches. GitHub Docs. 检索于 2025 年 2 月 19 日,来源: https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches ↩

  13. GitHub. (n.d.). Enforcing repository management policies in your enterprise: Blocking force pushes to all repositories. GitHub Enterprise Server 3.12 Docs. 检索于 2025 年 2 月 19 日,来源: https://docs.github.com/en/[email protected]/admin/enforcing-policies/enforcing-policies-for-your-enterprise/enforcing-repository-management-policies-in-your-enterprise#blocking-force-pushes-to-all-repositories ↩

  14. Foster, G. (n.d.). How to delete a commit from Git history. Graphite. 检索于 2025 年 2 月 19 日,来源: https://graphite.dev/guides/git-delete-commit-from-history ↩

  15. GitHub. (n.d.). Removing sensitive data from a repository. GitHub Docs. 检索于 2025 年 2 月 19 日,来源: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-19). 删除与找回 Git 提交:reset、rebase 和 reflog. https://caiborui.com/blog/2025-02-19-en-remove-and-recover-git-commits-zhtrans

@misc{borui2025-51f6b4fc,
  author = {borui},
  title = {{删除与找回 Git 提交:reset、rebase 和 reflog}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-19-en-remove-and-recover-git-commits-zhtrans}
}