How to force push to Gitlab

git push --force gitlab master Enumerating objects: 50, done. Counting objects: 100% (50/50), done. Delta compression using up to 8 threads Compressing objects: 100% (29/29), done. Writing objects: 100% (40/40), 12.22 KiB | 4.07 MiB/s, done. Total 40 (delta 26), reused 18 (delta 11) remote: GitLab: You are not allowed to force push code to a protected branch on this project. To gitlab.com:xxx/yyyy.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to ':xxx/yyyy.git' 

What should I do to push my commits to Gitlab? I use several remotes, and other remotes are OK and I was able to finish

git push --force some_other_remote master 
4

3 Answers

From the official documentation on dealing with Protected Branches:

  1. Navigate to your project’s Settings ➔ Repository
  2. Scroll to find the Protected branches section.
  3. From the Branch dropdown menu, select the branch you want to protect and click Protect.

Following the steps above, you should be greeted with a box similar to this one below.

Two configuration sections. One to set configurations and another that lists which branches on your GitLab repository are protected

There, you can click either:

  • "Allowed to force push" toggle button, or
  • the orange Unprotect button

for the branch you want to force push to, e.g., master.

If you don't want to navigate through the navigation bars, you can also fill out this URL template:

 

and replace <USERNAME> and <PROJECTNAME> with your specific username and project name, respectively.

Note, the "Allowed to force push" button is probably favored over un-selecting the Unprotect button because branch protection gives you additional safety from accidentally deleting your branch. But either option appears to work.

More help:

  • Community discussion on force pushing on branch
  • Official documentation to do so
2

As the GitLab documentation states: By default, a protected branch does four simple things:

  • It prevents its creation, if not already created, from everybody except users with Maintainer permission.
  • It prevents pushes from everybody except users with Allowed permission.
  • It prevents anyone from force pushing to the branch. <-----
  • It prevents anyone from deleting the branch.

So you need to unprotect the branch temporarily. Finally, return it to its original state.

For doing that :

  1. Navigate to your project’s Settings ➔ Repository
  2. Scroll to find the Protected branches section.
  3. Unprotected the branch which you want to force push
  4. Force push
  5. Revert all settings back in project’s Settings ➔ Repository (gitlab)
1
  1. Temporarily unprotect the master branch.
  2. Push as how you did before.
  3. Then restore the protection to the branch.
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like