问题库 问题库 how-to

Git 如何找回丢失 commit

快速答案

先查看 reflog:

bash
git reflog

找到目标提交后,建议先创建恢复分支:

bash
git switch -c recover-work <commit>

如果确认要把当前分支回到该提交:

bash
git reset --hard <commit>

推荐流程

  1. 先 git status,确认当前工作区是否干净。
  2. 用 git reflog 找到 reset、rebase、checkout 前的位置。
  3. 用 git switch -c recover-work <commit> 建分支,避免继续破坏现场。
  4. 确认内容正确后,再决定 merge、cherry-pick 或 reset。

常见坑

  • 直接 reset 到 reflog 位置,覆盖当前工作区未保存改动。
  • 以为 reflog 是永久记录;过期清理后可能无法恢复。
  • 只看 git log,看不到已经不在当前分支历史上的提交。

相关内容