受保护分支推送失败
错误信息
! [remote 拒绝] 主要 -> 主要 (受保护分支 Hook declined)
快速摘要
分支 (e.g. 主要) 是受保护 on remote — direct pushes 是不允许. 你必须 use 拉取请求.
为什么发生
分支是受保护 on GitHub/GitLab and 做不允许 direct pushes
你是 trying 到推送到主要但分支 protection 需要 PR
你的 account 做不有 permission 到 bypass 分支 protection
最小示例
✗ 错误代码
git push origin main # ! [remote rejected] main -> main (protected branch)✓ 修复代码
# Use a feature branch and PR instead
git checkout -b feature/my-change
git push -u origin feature/my-change
# Open a PR on GitHub to merge into main受保护分支需要拉取请求. 创建 feature 分支, 推送它, and 打开 PR for review.
如何诊断
检查仓库设置 for 分支 protection 规则
验证你是 pushing 到正确分支
检查如果你有 admin/maintainer permissions
如何修复
创建 feature 分支, 推送它, and 打开拉取请求到主要
如果你有 admin access, temporarily 禁用分支 protection, 推送, and re-启用
Ask 仓库 admin 到合并你的更改通过 PR
如何预防
从不推送 directly 到受保护分支 — use feature 分支 and PRs
检查分支 protection 规则之前 pushing