31

I am trying to push to master branch of a repo and I am failing to do so, since it is protected.
I tried to look into the project settings and do not see any option for protected branches. The only option I could see is members.

remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@gitlab.ins.risk.regn.net:cmd/release.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@gitlab.ins.risk.regn.net:cmd/release.git'

My repo has only one branch, with no contents in it so far.
I do see protected branches options of my other Repos but not for this specific one.
It is a new repo with no contents and with only default branch.
I have the master permission.
Unfortunately I am not able to upload the image here somehow.

Please suggest how to push code to master branch.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
maddie
  • 325
  • 1
  • 4
  • 12
  • 2
    Possible duplicate of [how to fix: you are not allowed to push code to protected branches on this project](http://stackoverflow.com/questions/32246503/how-to-fix-you-are-not-allowed-to-push-code-to-protected-branches-on-this-proje) – Hcorg May 05 '17 at 15:50

8 Answers8

32

12/17/2018

1. git push: "error: failed to push some refs to"

git push: error: failed to push some refs to

git push -f: "remote rejected"

git push -f: remote rejected

2. the branch is in a protected state and cannot be forced to operate.

Gitlab - Repository - Branches

Gitlab - Repository - Branches

3. temporarily remove branch protection.

Gitlab - Settings - Repository - Protected Branches - Unprotect

Gitlab - Settings - Repository - Protected Branches - Unprotect

4. try pushing again

git push -f

git push -f

5. may add protection

山茶树和葡萄树
  • 1,167
  • 1
  • 12
  • 14
13

with no contents in it so far

That means there is no master branch to protect yet, because the empty repo does not has one.

To "Enable/disable branch protection", you need to be Master or Owner of the GitLab project (which you are).

Make sure:

  • your first push is a git push -u origin master;
  • the remote origin does reference the right repo (git remote -v);
  • your local ssh key is the right one (ssh -T git@gitlab.ins.risk.regn.net);
  • you are a member of the cmd group.
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
8

In GitLab some branches can be protected. By default only 'master' user can commit to protected branches and master branch is protected by default.

You can turn on and off protection on selected branches in Project Settings (Go to project: "Settings" -> "Repository" -> "Expand" on "Protected branches" ).

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

7wick
  • 349
  • 2
  • 12
6

It means that you may have a master branch, but it is protected in project settings. See:

how to fix: you are not allowed to push code to protected branches on this project or https://gitlab.com/gitlab-com/support-forum/issues/207.

In order to access project settings and unprotect the branch, you need to have sufficient rights.

CoolMind
  • 16,738
  • 10
  • 131
  • 165
2

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

enter image description here

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

aristotll
  • 6,572
  • 5
  • 30
  • 47
nagi gee
  • 61
  • 2
1

Settings>Repo>Expand.. worked for me Few branches could be secured. Keep in mind the master branch is default protected. As a matter of course the master will be able to commit to protected branches.

Check out: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/51741

vine_J
  • 55
  • 4
0

I had a similar issue - a CI/CD pipeline/job for a project was complaining that it can't pull the code from a protected branch of a private repository on a self-hosted GitLab instance.

In my case I was able to manipulate the repositories and pull code directly with git because I had full administrative privileges in GitLab. In the end it turns out that GitLab Runner was not allowed to clone the repository because my user was not a direct member of the project group.

zorlem
  • 430
  • 3
  • 6
-2

Perhaps the master branch opens the protection. You need to select the developer to push in the protection branch settings.

  • Welcome to Stack Overflow! [Please use Answers exclusively to answer the question](//meta.stackoverflow.com/q/92107). To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have [sufficient reputation](//stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](//stackoverflow.com/help/privileges/comment). In the meantime, please do not use answers to post comments. – Tim Diekmann Jul 26 '18 at 09:06