用 git reflog 找回丢失的提交

在误重置、误变基或误删分支后找到并恢复提交。

macOS备份Git命令行低风险
运行 git reflog 列出最近的 HEAD 位置,再 reset 或 cherry-pick 需要的提交。

01命令

命令
$ git reflog

02作用说明

reflog 是每次引用移动的本地日志,包括已不再被任何分支引用的提交。它是命令行历史编辑的安全网,图形界面无法像它一样恢复游离或已丢弃的提交。

03风险

风险低风险
使用条件Git 2.20+ (install with Homebrew or Xcode Command Line Tools)
系统兼容性macOS with Git installed (Homebrew or Xcode Command Line Tools)
!注意: reflog 条目会在默认保留期后过期;请尽快恢复并把 SHA 另存。

04撤销

撤销
$ git reset --hard HEAD@{1}

05来源

  • git-reflog documentation
    Git · vendor-doc · 2026-09-20
    依据: Documents the reflog, which records where HEAD and branches have pointed.

最后验证: 2026-10-13 · 分类: 开发者

06相关方案