Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '[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      
like image 570
qazwsx Avatar asked Jan 08 '19 22:01

qazwsx


People also ask

How do I force a branch to push on github?

If you have a look at Git's official documentation, you will quickly notice that you can force this command. You can use the --force flag (or -f for short). This can look like an easy workaround when the git push command does not work, but it is rarely recommended — it's not the default behavior for a reason.

Is force push a good practice?

Force-pushing is a highly threatening and risky method if you're working in a common repository. Using this force pushing you should be able to force your local revision to the remote repository. But forcefully pushing to remote repo is not a good practice.


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:

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

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 https://gitlab.com/gitlab-com/support-forum/issues/93
  • Official documentation to do so https://docs.gitlab.com/ee/user/project/protected_branches.html
like image 57
Eric Leung Avatar answered Oct 21 '22 03:10

Eric Leung


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)
like image 6
Ali Maddi Avatar answered Oct 21 '22 03:10

Ali Maddi


  1. Temporarily unprotect the master branch.
  2. Push as how you did before.
  3. Then restore the protection to the branch.
like image 3
qazwsx Avatar answered Oct 21 '22 01:10

qazwsx