作为GitHub公开仓库所有者推送时遇“无法更新受保护引用”错误的解决方法
问题
我在GitHub创建了一个私有仓库,之后通过GitHub设置将其改为公开仓库,未进行其他更改。我使用Visual Studio开展开发工作,尝试推送本地提交时失败,控制台输出如下:
Remote: Error: GH013: Repository rule violations found for refs/heads/main.
Remote: Review all repository rules at http://github.com/xxx/xxx/rules?ref=refs%2Fheads%2Fmain
Remote:
Remote: - Cannot update this protected ref.
Remote:
To https://github.com/xxx/xxx
Error: main -> main (push declined due to repository rule violations)
failed to push some refs to 'https://github.com/xxx/xxx'
我尝试通过Git Bash手动推送,仍出现相同错误。我已在Visual Studio中登录GitHub账号,是该仓库的所有者,无协作者,且未添加任何自定义仓库规则或角色。
解决方案
- 检查分支保护规则:GitHub将私有仓库转为公开仓库时,会自动给默认分支(main)启用保护规则,哪怕你没手动添加。进入仓库的「Settings」→「Branches」→「Branch protection rules」,查看是否有针对main分支的规则,比如是否开启了「Require a pull request before merging」或者「Include administrators」选项。
- 调整分支保护设置:如果不需要这些规则,直接删除对应的分支保护规则;或者取消勾选「Include administrators」,这样仓库所有者可以绕过规则直接推送。
- 强制推送(谨慎操作):确认本地提交无误且无冲突的情况下,可以尝试执行强制推送命令:
git push -f origin main。注意:强制推送会覆盖远程main分支的内容,只有当你完全掌控仓库、没有其他协作者时再用。 - 验证远程仓库地址:执行
git remote -v检查本地仓库关联的远程URL是否正确,若地址错误,用git remote set-url origin https://github.com/xxx/xxx修正。
内容的提问来源于stack exchange,提问作者Viktor Vošček
相关产品推荐
相关产品推荐

