How to force push to Gitlab

qazwsx picture qazwsx · Jan 8, 2019 · Viewed 15.7k times · Source
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 '[email protected]: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      

Answer

Eric Leung picture Eric Leung · Apr 24, 2019

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.

Image sources: https://docs.gitlab.com/ee/user/project/protected_branches.html

There, you can click the orange Unprotect button for the branch you want to force push to, i.e., master.

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

https://gitlab.com/<USERNAME>/<PROJECTNAME>/settings/repository#js-protected-branches-settings

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

More help: