首页/开发/Git 与协作速查/找回悬空提交与误删数据
Git 基础与命令88 条

找回悬空提交与误删数据

恢复流程:fsck --lost-found 或 reflog 定位无引用提交,git show 核对内容后用 branch/reset 接回历史。

Git 与协作速查开发更新于 2026-10-12
场景误 reset、误删分支、误 amend
定位git fsck --lost-found 或 git reflog
接回git branch <名称> <哈希>;git reset --hard <哈希>

常用示例

常用示例
git fsck --lost-found
git show <dangling-commit-hash>
git branch rescued <hash>
git reflog | head -20
找回后尽快挂到分支或标签上,避免后续 gc 把对象清掉。

易错点

  • 未提交到对象库的改动(只在编辑器或工作区)无法用这套方法找回
  • 对象一旦被 gc --prune=now 清理便无法恢复,找回动作要早

数据来源与参考

  • Git 官方文档:git-fsck(--lost-found)
  • Pro Git 2nd Edition · 10.4 数据恢复
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌