Skip to content

无效对象 Name

错误信息

                fatal: 无效对象 name 'HEAD'
              

快速摘要

HEAD 做不点到有效提交 — 大多数通常因为否提交有是制作 yet.

为什么发生

  • 仓库是只是 initialized (git init) 使用否提交

  • 所有提交是移除 (e.g. git reset --困难 on 首先提交)

  • .git directory 是 corrupted

最小示例

✗ 错误代码
git log  # fatal: invalid object name 'HEAD' (no commits yet)
✓ 修复代码
git add .
git commit -m "Initial commit"
git log  # now works

freshly initialized 仓库有否提交, 所以 HEAD 是无效. 制作你的首先提交到 fix 它.

如何诊断

  • 运行 git log — 如果它失败, 那里是否提交

  • 运行 git 状态 — 它应该 say '否提交 yet'

  • 检查如果 .git/HEAD exists and 是有效

如何修复

  • 制作你的首先提交: git 添加 . && git 提交 -m "Initial commit"

  • 如果提交是 accidentally 移除, 检查 git reflog for recovery

  • 如果 corrupted, re-克隆从 remote

如何预防

  • 总是制作初始提交之后 git init 之前 using HEAD 引用

  • 是 careful 使用 git reset --困难 on 仓库使用少数提交

相关资源

相关术语

相关课程

← 返回语言