48

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,262,500
  • 529
  • 4,410
  • 5,250
maddie
  • 505
  • 1
  • 6
  • 13
  • 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

10 Answers10

56

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

山茶树和葡萄树
  • 2,050
  • 1
  • 18
  • 18
  • @Coderman This is the permission management of the git repository and must be set this way. – 山茶树和葡萄树 Apr 02 '20 at 00:23
  • Current version of GitLab has a slider to allow force pushed to currently protected branches. – Pjotr Sep 23 '21 at 09:58
  • `git push -f` is a dangerous operation, do not perform it on the public branch, only on your own temporary branch. Under normal circumstances, there are prompts on git bash, follow the prompts, such as performing the `pull` operation before `push` on multiple collaborative branches. Instead of force pushing, unless your team really needs to force the operation (note that it is your team, not you). – 山茶树和葡萄树 May 31 '23 at 01:43
15

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,262,500
  • 529
  • 4,410
  • 5,250
10

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
  • 411
  • 4
  • 17
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
  • 26,736
  • 15
  • 188
  • 224
3

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
  • 8,694
  • 6
  • 33
  • 53
nagi gee
  • 81
  • 1
  • 2
2

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
  • 123
  • 6
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
  • 450
  • 3
  • 6
0

I know it's a little old, but for me, remote was set with a prefixed user that a devops made to deploy, so I had to change remote url to don't use the username it was set, or you can change to yours.

Marco
  • 2,757
  • 1
  • 19
  • 24
0

We decided to push to a different branch and then create a merge request from that different branch into main using the GitLab UI.

git push -fu origin differentbranch  

Our reason for doing this is that locking down branches like main is a good policy. We do not want to weaken that good policy by allowing manual overrides to it.

The merge request process in the GitLab UI only took a few seconds, so that our approach might even be faster than the other approaches offered by other answers to this old OP.

CodeMed
  • 9,527
  • 70
  • 212
  • 364
-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