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

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://git-scm.com/docs/git-update-ref

解决方案

  1. 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
  2. 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

无效尝试

常见但无效的做法:

  1. 50% 失败

    Running git fsck and then git gc without understanding the corruption source can remove necessary objects and worsen the repository state.

  2. 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.