ChatGPT解决这个技术问题 Extra ChatGPT

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

I have a problem when I push my codes to git while I have developer access in my project, but everything is okay when I have master access. Where is the problem come from? And how to fix it?

Error message:

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

Hcorg's answer is a good solution. There is another problem with it. If the project has just create and it has no branch yet. If click the "Protected branches", it will redirect to the project home page. Create a branch will work.
See also stackoverflow.com/a/61964599/6309, with GitLab 13.0 (May 2020), where you can enable group-level default branch protection.

U
Uwe Keim

there's no problem - everything works as expected.

In GitLab some branches can be protected. By default only Maintainer/Owner users can commit to protected branches (see permissions docs). master branch is protected by default - it forces developers to issue merge requests to be validated by project maintainers before integrating them into main code.

You can turn on and off protection on selected branches in Project Settings (where exactly depends on GitLab version - see instructions below).

On the same settings page you can also allow developers to push into the protected branches. With this setting on, protection will be limited to rejecting operations requiring git push --force (rebase etc.)

Since GitLab 9.3

Go to project: "Settings" → "Repository" → "Expand" on "Protected branches"

https://i.stack.imgur.com/e82Pn.png

I'm not really sure when this change was introduced, screenshots are from 10.3 version.

Now you can select who is allowed to merge or push into selected branches (for example: you can turn off pushes to master at all, forcing all changes to branch to be made via Merge Requests). Or you can click "Unprotect" to completely remove protection from branch.

Since GitLab 9.0

Similarly to GitLab 9.3, but no need to click "Expand" - everything is already expanded:

Go to project: "Settings" → "Repository" → scroll down to "Protected branches".

https://i.stack.imgur.com/rr8N2.png

Pre GitLab 9.0

Project: "Settings" → "Protected branches" (if you are at least 'Master' of given project).

https://i.stack.imgur.com/Whjbb.jpg

Then click on "Unprotect" or "Developers can push":

https://i.stack.imgur.com/R5sIt.jpg


Don't forget that there may be required some permissions. As stated in docs.gitlab.com/ee/user/project/protected_branches.html, at least 'Master permission level'. In my case pressing on a settings wheel shows only 'Leave Project` option.
For some reason I suddenly had to add myself as a master user for my own project.
I got this problem because I was NOT a member of my OWN project and i already pushed on this project... To change it, in tour project, click the gear, Members, search your user, give it a role and click "Add users to project".
Strange, me too, had to include myself on a personnal project on gitlab.com
It is good if you are the only maintainer or developer, so you can change the setting and play around with it. But if there is a team working on the repo, then it is not a good practice of changing the protection of the repo.
k
krekto

for the GitLab Enterprise Edition 9.3.0

By default, master branch is protected so unprotect :)

1-Select you "project"

2-Select "Repository"

3-Select "branches"

4-Select "Project Settings"

5-In "Protected Branches" click to "expand"

6-and after click in "unprotect" button


I didn't have "branches" because I didn't create any file on this repository yet. I've created Readme.md and branches appeared.
For the passersby... please don't do this. Even if you work in a small org/company it opens up serious security issues
V
VonC

Alternative solution, with GitLab 13.11 (April 2021)

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.


a
aloisdg

I was on Windows when this problem appeared.

The error is strange because it happens before I could enter my username and my password. What if there was a cache or something like this? I dig it online and found this 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.

In the Windows Credentials, I found two GitLab entries for an old account. I remove both and now it works!

The panel:

https://i.stack.imgur.com/DtqSt.png


@YanickSenn You are welcome. I lost a lot of time on this one. Glad it helps.
V
Vahid F

I have encountered this error on "an empty branch" on my local gitlab server. Some people mentioned that "you can not push for the first time on an empty branch". I tried to create a simple README file on the gitlab via my browser. Then everything fixed amazingly and the problem sorted out!! I mention that I was the master and the branch was not protected.


This is odd for me and I consider this issue as a gitlab bug. It's unacceptable for me to have no permission to push into an empty repo. I hope git guys have an answer for it.
B
Barath Ravichander

Try making changes as per link

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

make the project unprotected for maintainer or developer for you to commit


m
mochadwi

This is considered as features in Gitlab.

https://i.stack.imgur.com/iY3XZ.png


Actually this isn't unfortunate at all. It's definitely a good thing. It's an extra layer of protection.
r
ravthiru

When you error message remote: You are not allowed to push code to this project. and The requested URL returned error: 403

Try setting the git user,

To prompt username before pushing the Code, use

$ git config --local credential.helper "" 

After entering Username and Password and successful login

$ git push

S
Stephen Ostermiller

I experienced the same problem on my repository. I'm the master of the repository, but I had such an error.

I've unprotected my project and then re-protected again, and the error is gone.

We had upgraded the gitlab version between my previous push and the problematic one. I suppose that this upgrade has created the bug.


k
kris

Simple solution for this problem to have quick chat with person who has owner role in gitlab. He can push one file READ.md or similar to just start with. Later, everything will be working as earlier.


If possible, try to get the owner role in repository. Once you have owner role, you can commit directly to master. It's annoying but preventive hook not to create unwanted new projects.There is no hack around until owner of repo push the first file or you have owner role. Hope this helps.
C
Curious Watcher

For me, it was an issue of choosing Developer rather than Maintainer position while creating a personal access token.

Choosing Maintainer solved the situation.


g
gogasca

The above solutions explain clearly what the problem is; when you don't have control over the repo, the best way to submit your code is to create a Fork of the original repo and submit your code to this new repo so later you can push it to the original one.