631

I have a problem when I push my code 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 ...

starball
59k52 gold badges309 silver badges1k bronze badges
asked Aug 27, 2015 at 10:08
2
  • 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. Commented Mar 20, 2017 at 9:37
  • See also stackoverflow.com/a/61964599/6309, with GitLab 13.0 (May 2020), where you can enable group-level default branch protection. Commented May 22, 2020 at 22:08

19 Answers 19

951

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

Uwe Keim
40.9k61 gold badges193 silver badges308 bronze badges
answered Aug 28, 2015 at 8:56
Sign up to request clarification or add additional context in comments.

17 Comments

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".
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.
there's no problem - everything works as expected That is a horrible comment. The problem is the error message in the first place that leads people here instead of directly to the solution!
|
62

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

answered Jun 19, 2017 at 15:37

3 Comments

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
Downvoted because "just disable the security feature" is always the last solution you want to try.
25

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.


Also, GitLab 16.2 (July 2023) adds:

Allow initial push to protected branches

In previous versions of GitLab, when a default branch was fully protected, only project maintainers and owners could push an initial commit to a default branch.

This caused problems for developers who created a new project, but couldn’t push an initial commit to it because only the default branch existed.

With the Fully protected after initial push setting, developers can push the initial commit to the default branch of a repository, but cannot push any commits to the default branch afterward. Similar to when a branch is fully protected, project maintainers can always push to the default branch but no one can force push.

https://about.gitlab.com/images/16_2/govern-initial-push-protected-branch.png -- Allow initial push to protected branches

See Documentation and Issue.

answered Apr 24, 2021 at 16:21

Comments

12

In my case the above one works but just to clarify the steps with little more details so I just followed the below steps to remove the above error and push my code

Step-1: Go to your project settings

Step-2: Then click on repository

Step-3: Go to protected branches, expand and enable Allowed to force push as mentioned in below screenshot

enter image description here

Here successfullly pushed my code without any issue

enter image description here

answered Nov 12, 2023 at 14:38

1 Comment

I like your graphic approach to the answer. Can you please change the recommendation from "Unprotecting" the branch to "Allowed to force push" so I can upvote you?
8

To the settings after opening the branch in git. Then allow force push.

Allow to force push option

Suraj Rao
29.7k11 gold badges96 silver badges104 bronze badges
answered Jul 28, 2022 at 4:46

Comments

7

Enabling force Push works for me too.

16.2 does not *upport Push, so this works for me too:

enter image description here

BenjyTec
12.5k4 gold badges27 silver badges56 bronze badges
answered Nov 7, 2023 at 9:22

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
6

When you have 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
answered Feb 2, 2022 at 14:10

Comments

4

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

answered Oct 16, 2019 at 23:32

1 Comment

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

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

answered Mar 20, 2020 at 8:07

1 Comment

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

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.

answered Jun 30, 2018 at 5:29

1 Comment

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

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

Choosing Maintainer solved the situation.

answered Apr 6, 2022 at 17:50

Comments

2

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

answered Jan 10, 2019 at 7:27

Comments

1

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.

Stephen Ostermiller
25.8k18 gold badges96 silver badges116 bronze badges
answered Jan 27, 2017 at 9:34

Comments

1

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.

answered Sep 13, 2019 at 16:14

1 Comment

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

Solution: Rename your branch

I encountered this error when I was trying to push my own new branch so I was surprised. It seemed it was seeing the branch as protected. It turns out that the name pattern my branch started with had the same pattern as the protected "main" branch for the sprint. So I'm guessing there's some form of regex protecting all branches that start that way. Hence all subsequent sprint branches of that pattern gets protected.

sprint branches: abc_2023_1, abc_2023_2, abc_2023_3
my initial branch name: abc_2023_3_launch_that_awesome_rocket
my renamed branch name: launch_that_awesome_rocket

Was asking myself how is it that I've made countless branches and never started them this way except this one. Why did I do that...lol.(I checked out to a new branch via intelliJ and continued with the prefilled name instead of clearing it😬)

Thanks to my teammate who pointed the pattern thing out.

answered Nov 27, 2023 at 13:30

Comments

0

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.

answered Dec 13, 2018 at 22:51

1 Comment

The error message still comes up if you try to push from that forked repo. You still one of the solutions / workarounds like create a merge request or allow force pushing.
0

I encountered a similar issue. However, the problem was that the repository was accidentally archived! You can check by going to the repository (in Gitlab at least) and it showed the following message "Archived project! Repository and other project resources are read-only." In order to push to it, we had to unarchive it.

answered Oct 13, 2022 at 14:30

Comments

0

For me the problem was the user trying to push simply was not invited to the project. I had to go to Project Information > Members and invite the member as a developer so they had access to push.

answered Jan 3, 2023 at 22:10

Comments

0
cigien
61k11 gold badges85 silver badges122 bronze badges
answered Aug 30, 2023 at 11:51

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.