Fix GitLab error: "you are not allowed to push code to protected branches on this project"?

Fix GitLab error: "you are not allowed to push code to protected branches on this project"?

当我在我的项目中拥有开发人员访问权限时将我的代码推送到 git 时出现问题,但是当我拥有主访问权限时一切正常。问题从何而来?以及如何修复它?

错误信息:

error: You are not allowed to push code to protected branches on this project.
...
error: failed to push some refs to ...

没问题 - 一切正常。

在 GitLab 中,一些分支可以被保护。默认情况下,只有 Maintainer/Owner 用户可以提交到受保护的分支(参见 permissions docs)。 master 分支在默认情况下受到保护 - 它强制开发人员发出合并请求以供项目维护人员验证,然后再将它们集成到主代码中。

您可以在项目设置中打开和关闭 selected 分支的保护(具体取决于 GitLab 版本 - 请参阅下面的说明)。

在同一设置页面上,您还可以允许开发人员推送到受保护的分支。启用此设置后,保护将仅限于拒绝需要 git push --force(变基等)

的操作

自 GitLab 9.3

转到项目:"Settings" → "Repository" → "Expand" "Protected branches"

我不太确定何时引入此更改,屏幕截图来自 10.3 版本。

现在您可以 select 谁被允许合并或推送到 selected 分支(例如:您可以完全关闭对 master 的推送,强制所有更改通过合并请求创建分支)。或者您可以单击 "Unprotect" 完全取消对分支的保护。

自 GitLab 9.0

与 GitLab 9.3 类似,但无需单击 "Expand" - 一切都已展开:

转到项目:"Settings" → "Repository" → 向下滚动到 "Protected branches"。

GitLab 9.0 之前版本

项目:"Settings" → "Protected branches"(如果您至少是给定项目的 'Master')。

然后点击"Unprotect"或"Developers can push":

我在我的存储库中遇到了同样的问题。我是版本库的主人,但是我有这样的错误。

我解除了对项目的保护,然后再次 re-protected,错误消失了。

我们已经升级了我之前推送和有问题的推送之间的 gitlab 版本。我想是这次升级造成了这个错误。

对于 GitLab 企业版 9.3.0

默认情况下,master 分支是受保护的 所以取消保护:)

1-Select你"project"

2-Select "Repository"

3-Select "branches"

4-Select "Project Settings"

5-在"Protected Branches"点击"expand"

6-点击 "unprotect" 按钮后

我在本地 gitlab 服务器上的 "an empty branch" 上遇到过这个错误。有人提到 "you can not push for the first time on an empty branch"。我试图通过浏览器在 gitlab 上创建一个简单的自述文件。然后一切都得到了惊人的解决,问题也解决了!提一下我是master,分支没有保护

以上解决方案清楚说明问题所在;当您无法控制 repo 时,提交代码的最佳方式是创建原始 repo 的 Fork 并将您的代码提交到这个新 repo,以便稍后您可以将其推送到原始 repo。

尝试根据 link

进行更改

https://docs.gitlab.com/ee/user/project/protected_branches.html

使项目不受维护者或开发者的保护,供您提交

这个问题的简单解决方案是与在 gitlab 中拥有所有者角色的人进行快速聊天。他可以推送一个文件 READ.md 或类似的文件来开始。稍后,一切都会像以前一样工作。

出现这个问题时我正在 Windows。

这个错误很奇怪,因为它发生在我输入用户名和密码之前。如果有缓存或类似的东西怎么办?我在网上挖了一下,发现了这个 answer on gitlab's support forum:

I open "Control Panel => User Accounts => Manage your credentials => Windows Credentials" I found two for https://@github.com and one was the wrong user. I deleted it and on the next "git push" I was reprompted and provided the correct credentials and it worked! Some other notes - this could have happened with any git remote.

在 Windows 凭据中,我发现了一个旧帐户的两个 GitLab 条目。我删除了两者,现在可以使用了!

面板:

这被认为是 Gitlab 中的功能。

Maintainer / Owner 访问永远无法强制再次推送默认和受保护的分支,如 this docs 中所述

替代解决方案,GitLab 13.11(2021 年 4 月)

Force push option for protected branches

It’s best practice to prevent force push on Git repos, but exceptional cases may occasionally require it.

Temporarily removing branch protections in order to conduct a force push may not always be ideal as it requires maintainer access, and causes the settings for branch protection to be lost.

GitLab 13.11 introduces a new Allow force push setting for protected branches, which enables users in the Allowed to push list to force push.

See Documentation and Issue.

当您收到错误消息 remote: You are not allowed to push code to this project.The requested URL returned error: 403

尝试设置 git 用户,

要在推送代码之前提示用户名,请使用

$ git config --local credential.helper "" 

输入用户名和密码并成功登录后

$ git push

对我来说,这是在创建个人访问令牌时选择 Developer 而不是 Maintainer 位置的问题。

选择Maintainer解决了这个问题。