git runtime_error ai_generated partial

fatal: cannot update ref 'refs/heads/main': trying to write non-commit object to branch 'main'

ID: git/fatal-override-branch-restriction

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
1Evidence
2024-01-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
git 2.35.0 active
git 2.40.0 active
git 2.44.0 active

Root Cause

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

中文

提交或更新操作试图将非提交对象(如树或 blob)直接写入分支引用,违反了 Git 的对象模型。

Official Documentation

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

Workarounds

  1. 75% success 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 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. 80% success 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
    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. 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

Dead Ends

Common approaches that don't work:

  1. 50% fail

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

  2. 90% fail

    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.