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

BranchGitlabGit CommitGit PushGit Add

Branch Problem Overview


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

Branch Solutions


Solution 1 - Branch

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"

enter image description here

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".

enter image description here

Pre GitLab 9.0

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

Settings → Protected branches

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

enter image description here

Solution 2 - Branch

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

Solution 3 - Branch

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. > > https://about.gitlab.com/images/13_11/code_owners_approval_new_protected_branch_v13_10.png -- Force push option for protected branches > > See Documentation and Issue.

Solution 4 - Branch

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:

enter image description here

Solution 5 - Branch

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.

Solution 6 - Branch

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

Solution 7 - Branch

This is considered as features in Gitlab.

Maintainer / Owner access is never able to force push again for default & protected branch, as stated in this docs enter image description here

Solution 8 - Branch

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.

Solution 9 - Branch

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.

Solution 10 - Branch

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

Solution 11 - Branch

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

Choosing Maintainer solved the situation.

Solution 12 - Branch

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFachrein Rachim MuhamadView Question on Stackoverflow
Solution 1 - BranchHcorgView Answer on Stackoverflow
Solution 2 - BranchkrektoView Answer on Stackoverflow
Solution 3 - BranchVonCView Answer on Stackoverflow
Solution 4 - BranchaloisdgView Answer on Stackoverflow
Solution 5 - BranchVahid FView Answer on Stackoverflow
Solution 6 - BranchBarath RavichanderView Answer on Stackoverflow
Solution 7 - BranchmochadwiView Answer on Stackoverflow
Solution 8 - Branchjean pierre huartView Answer on Stackoverflow
Solution 9 - BranchkrisView Answer on Stackoverflow
Solution 10 - BranchravthiruView Answer on Stackoverflow
Solution 11 - BranchCurious WatcherView Answer on Stackoverflow
Solution 12 - BranchgogascaView Answer on Stackoverflow