Skip to content

git reflog:条目已过期并被清理

快速摘要

你尝试通过 git reflog 恢复提交,但该条目已被 git gc 清理 —— 提交哈希不再存在于 reflog 中。

为什么发生

  • git gc 自动运行并清理了超过 gc.reflogExpire(默认 90 天)的 reflog 条目

  • 你运行了 git reflog expire --expire=now 或 git gc --prune=now,激进地删除了所有旧 reflog 条目

  • reflog 被手动清除,或仓库是全新克隆的,没有原始 reflog 历史

最小示例

✗ 错误代码
git reflog  # entry for abc123 is gone after git gc --prune=now
git reset --hard abc123  # fatal: ambiguous argument
✓ 修复代码
git fsck --lost-found  # find dangling commit objects
git reset --hard <found-hash>  # recover from lost-found if present

git fsck --lost-found 搜索对象数据库中尚未从磁盘清理的悬空提交,提供最后的恢复途径。

如何诊断

  • 运行 git reflog show <branch> 检查该条目是否仍存在于分支 reflog 中

  • 使用 git fsck --lost-found 搜索仍可从对象存储恢复的悬空提交

  • 检查 git config gc.reflogExpire 和 gc.reflogExpireUnreachable 了解清理计划

如何修复

  • 用 git fsck --lost-found 搜索悬空提交,如果对象仍存在则从 .git/lost-found 恢复

  • 如果远程仍有该提交,用 git fetch origin <commit-hash> 在本地恢复该对象

  • 如果提交确实已丢失,从备份重建或手动重新应用已知修改

如何预防

  • 将 gc.reflogExpire 设置为更长的期限,或设置 git config gc.auto 0 禁用自动垃圾回收

  • 立即用 git tag 标记重要提交,防止它们作为不可达对象被清理

相关资源

相关术语

相关课程

← 返回语言