git
runtime_error
ai_generated
partial
致命错误:无法更新引用 'refs/heads/main':试图将非提交对象写入分支 'main'
fatal: cannot update ref 'refs/heads/main': trying to write non-commit object to branch 'main'
ID: git/fatal-override-branch-restriction
80%修复率
85%置信度
1证据数
2024-01-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| git 2.35.0 | active | — | — | — |
| git 2.40.0 | active | — | — | — |
| git 2.44.0 | active | — | — | — |
根因分析
提交或更新操作试图将非提交对象(如树或 blob)直接写入分支引用,违反了 Git 的对象模型。
English
A commit or update operation is attempting to write a non-commit object (like a tree or blob) directly to a branch reference, which violates Git's object model.
官方文档
https://git-scm.com/docs/git-update-ref解决方案
-
git reflog --all | grep <commit-hash> # Find the correct commit object, then use git branch -f main <correct-commit> to reset the branch to a valid commit
-
git update-ref -m 'reset to valid commit' refs/heads/main <valid-commit-hash> # Directly update the ref to a known good commit from reflog or backup
无效尝试
常见但无效的做法:
-
50% 失败
Running git fsck and then git gc without understanding the corruption source can remove necessary objects and worsen the repository state.
-
90% 失败
Using git update-ref -d to delete the branch reference entirely loses all commits on that branch, which is irreversible unless you have a backup.