How to force push a reset to remote repository?
Asked Answered
git
U

10

108

Our remote master branch somehow got messed up. Current development code is on the master branch along with the latest commits. Obviously, the development code is not ready for the master branch.

So on my local repository, I did a reset to the latest tag, git reset --hard (Tag). The master branch is now correct on my local repository. Now when I try to push the changes on to the remote repository, git push origin master, I get an error:

To (REMOTE GIT REPOSITORY LOCATION)
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '(REMOTE GIT REPOSITORY LOCATION)'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

So after looking around I found out the --force option. So I did a force push on to the remote repository, git push --force origin master, and I still got an error:

Total 0 (delta 0), reused 0 (delta 0)
remote: error: denying non-fast-forward refs/heads/master (you should pull first)
To (REMOTE GIT REPOSITORY LOCATION)
 ! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to '(REMOTE GIT REPOSITORY LOCATION)'

I can't do a pull on master, because it contains development code which can't be on master.

Unadvised answered 11/5, 2012 at 1:9 Comment(6)
I think the message means that you don't have the rights to do a non-fast-forward push.Fiorin
You were correct, thanks. In the config file for the repository on remote, denyNonFastforwards = true. I changed it to false, pushed my changes and then changed it back to true. Thanks again everyone, for the help.Unadvised
@Unadvised please mark svick's answer as acceptedLahomalahore
@Unadvised did svick's answer work for you or not?Doolittle
For those who need details on how to disable denyNonFastForwards like samwell did, more instructions can be found here: https://mcmap.net/q/12352/-force-git-push-on-sourceforgeBirthwort
Note: If trying to push a non-commit object to refs/heads/ is not allowed. This can be a tag (if annotated). You can do git push -f origin mytag^0:main to push the commit the tag points to instead. That will work.Stockton
F
166

The message means that you're not allowed to do non-fast-forward push.

Your remote repository has most likely denyNonFastforwards = true in its config. If you change that, git push --force should work.

To change the setting, you need access to the machine with the remote repository. From there, do git config receive.denynonfastforwards false.

Fiorin answered 11/5, 2012 at 1:9 Comment(9)
Can you do a git config for a server? Or perhaps you were using that metaphorically. To play with these ideas I created a test repo in /opt/git (my git server space) and then I modified this setting in /opt/git/the_repo/the_repo.git/config. But once done the git push --force origin SHA:branch worked as required.Dillman
The error message will have a line that starts with "error: failed to push some refs to <your repository>" where <your repository> is path ending in .git which is a directory containing a file called "config". This "config" file is where you can set denyNonFastforwards = falseConjunctive
@Conjunctive but if someone do not have access to the server?Firmin
@emery's comment is valuable. Sometimes the folder on the server will have its origin set to something like /srv/git/repo.git. This is the config that has denyNonFastForwards set, not the application folder.Excision
@hsalimi If you don't have access to the server then you need to contact the server admin and have them temporarily turn it off so you can force push and then turn it back on. It is unlikely most are in a position to do this though. It may be more common in an internal environment with your own hosting team.Excision
The key here is to check the config in the remote repository, not the local repository.Suborbital
This is initially frustrating, but the beauty of it is: the remote is totally protected by default, and if you as the developer are intentionally and correctly doing rebases, you can override this config to allow the dangerous behavior. Rebasing is something every git user should know how to do - and know when not to do. doc1 doc2Hulton
Since nobody mentioned it so far I would like to write when denynonfastforwards is enabled on behind. In my case it is due to command git init --bare --shared=.... So when --shared option is used within init command denynonfastforwards is set to true (it is also mentioned in doc: git-scm.com/docs/git-init). In my case I use mirroring of repo as-is so I want to force override it.Novelist
This flag has some annoying effect on commit-with-amend feature. If you use the commit-with-amend is too frequently, then you have to turn the feature off, otherwise you have to go to the remove repository each time you want to amend the last commit.Recusancy
W
17

The remote doesn't allow non-fast-forwards.

Your best option is to git revert all of the commits that shouldn't be there and be more careful in future.

git revert [commit] will create a new commit that undoes whatever [commit] did.

Wideranging answered 11/5, 2012 at 1:29 Comment(2)
It was some settings on the remote repository that blocked all non-fast-forward changes.Unadvised
If you do this and need to reapply commits the history isn't removed in a revert just the code changes, and you won't be able to cherry pick the commits or mergeDandrea
B
13

Try using the -f flag and putting it after the remote branch name.

git push origin master -f

Bibliotheca answered 11/5, 2012 at 1:12 Comment(1)
Nope, that didn't work either. I also tried git push -f origin master and same result. Both times I tried it I got the second version of the error message.Unadvised
C
13

Steps to permanently enable force push in the following style

git push -f myrepo my-branch

Edit the file named "config" in the folder ending in ".git" on your remote repository

In git's command-line output from the failed push, look for the line that says something like:

error: failed to push some refs to 'ssh://[email protected]/srv/git/myrepo.git

then

ssh [email protected]
cd /srv/git/myrepo.git
vi config

Set "denyNonFastforwards" to false

In "config", set

[receive]
        denyNonFastforwards = false

Now you can push from your local machine with -f

git push -f myrepo my-branch
Conjunctive answered 25/3, 2016 at 20:31 Comment(5)
How to do this without access to SSH to bare git repo?Riha
Maybe use the git revert command as richo suggests? If you back up the current state of the repo first, you can still merge your code in moving forward.Conjunctive
git revert is kind of complicated when you have merges. To be more complicated my case has 3 merge, of which one is with very old ~20 commit diverged from develop, 2nd is kind of merge from master - ugly as hell.Riha
Maybe the solution is to reset to desired state, backup (stash), pull again, and apply backup (stash).Riha
mrW you can still merge the code base you want on top of a reverted/pulled code baseConjunctive
H
3

The best way around this is to delete the remote branch and re-send it:

git push origin master --delete
git push origin master
Hipparchus answered 5/9, 2018 at 18:56 Comment(1)
the first command will fail with ! [remote rejected] master (deletion of the current branch prohibited)Wintergreen
A
2

You're not allowed to do git push that is not fast-forward.

  1. If the remote is GitHub, go to https://github.com/$USER/$REPO/settings/branches and un-protect the branch in question.

    enter image description here

    You have to be admin of the repo to do that.

  2. If the remote is your own git server, run git config receive.denynonfastforwards false there.

Amadaamadas answered 25/10, 2016 at 13:14 Comment(1)
Be aware that for Git Hub Enterprise instances, pushes to the default branch (usually "master") can be disabled at the instance-level. This means that even if "master" is not protected, and even if you are a site-admin, you will not be able to do force-pushes to the default branch. Assuming you have permissions, you can temporarily get around this by switching the default branch to something else, doing your force-push, and then switching back.Intrigante
R
1

For me, @svick 's hint pointed in the right direction. Since the git server I wanted to modify is actually my box, I logged into it and did a git config --global receive.denynonfastforwards false to change all repos to accept a forced non-ff push. Didn't work out of the box. What I found was that in the config there already was receive.denynonfastforwards=true set, and it couldn't be erased with git config --global --unset receive.denynonfastforwards. Doing the edit in the repo manually (vi config) worked, though.

Rapacious answered 11/5, 2019 at 19:44 Comment(0)
B
0

The problem occurs as the current branch isn't configured properly for the PULL. First check whether the upstream branch is properly configured for the pull using - git remote show origin. You can find it under the section - Local branches configured for 'git pull':. If not, configure it using:

git config branch.MYBRANCH.merge refs/heads/MYBRANCH

Give appropriate branch name for the place holder - MYBRANCH

Bema answered 3/3, 2016 at 10:55 Comment(0)
S
0

I'm using this group of commands to reset my remote repo, this will re-initialize your local repo and relink with your remote repo then force push the updates.

I think this way won't work in your case, but may be useful for someone else

go to the source folder then run the commands : note that https://github.com/*.git is your remote repo link

git init
git remote add origin https://github.com/*.git
git add .
git commit -m "initial commit"
git push origin master -f
git push --set-upstream origin master

**Note: this will clear all your git history on your master branch**

Schrader answered 16/4, 2018 at 15:51 Comment(0)
P
0

I solved by removing the master branch from protected and also default which is just over proted branch rules in setting of a repository.

Plowman answered 6/5, 2020 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.