# 致命错误：无法更新引用 'refs/heads/main'：试图将非提交对象写入分支 'main'

- **ID:** `git/fatal-override-branch-restriction`
- **领域:** git
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| git 2.35.0 | active | — | — |
| git 2.40.0 | active | — | — |
| git 2.44.0 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — Running git fsck and then git gc without understanding the corruption source can remove necessary objects and worsen the repository state. (50% 失败率)
- **** — 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. (90% 失败率)
