20

I'm trying to undo some changes that have already been pushed to a remote repository, and I've done so locally with

git reset --hard COMMIT-HASH

But now it won't let me push without pulling first, which of course defeats the purpose. I've tried:

git push -f

Which errors out with:

Total 0 (delta 0), reused 0 (delta 0)
remote: error: denying non-fast-forward refs/heads/master (you should pull first)
To [email protected]:/yyy.git
 ! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:/yyy.git'

So how do I get my new, correct version of the branch to the remote?

4
  • 1
    Does this help you?
    – user377628
    Aug 6, 2012 at 14:15
  • @Hassan - Sorry, no. I've already rolled back the commits I don't want, the issue is now pushing that to the remote successfully.
    – Will
    Aug 6, 2012 at 14:16
  • If you've already rolled back the commits on the git server, then why can't you pull? Aug 6, 2012 at 14:19
  • possible duplicate of Git reset --hard and a remote repository
    – tripleee
    Aug 6, 2012 at 16:16

3 Answers 3

26

From the git config man page:

receive.denyNonFastForwards

If set to true, git-receive-pack will deny a ref update which is not a fast-forward. Use this to prevent such an update via a push, even if that push is forced. This configuration variable is set when initializing a shared repository.

The server you are trying to push to has this setting enabled. So, short answer, is that in this case you will not be able to git push --force.


To get the correct version of the branch to the remote, you will have to make a new commit to the tip of the branch that gets it to the correct state. If you are currently at the commit for the correct state, you can run the following:

$ git reset --soft <remote>/<branch>    # point the ref back to the remote, but
                                        #   keep the index and working tree

$ git commit                            # make the 'correction' commit
$ git push
3

Does your server disallow non-fast forward push?

git config file

[receive]
denyNonFastforwards = true
2
  • 1
    It seems disabled by default with a warning: "We don't recommend changing these settings unless you know what you are doing. Beanstalk provides sensible defaults that work for most users out of the box." ... although enabling it and force pushing does solve the problem
    – Will
    Aug 6, 2012 at 14:19
  • Force pushing in branches other than master is essential when you use topic branches. Merging two branches may lead to code loses as it compares only tips of given branches. Rebasing crawls through all commits since the newest common ancestor and gives better results, but requires rewriting commits which can't be handled by fast-forwards. Novice git users don't know what rebasing is, so this option is good for them.
    – skalee
    Mar 21, 2013 at 13:24
0

The best way to undo changes in git is using git revert command.

To undo your last commit: git revert HEAD^

it will undo changes made in last commit, then creates a new commit on top of it.

Hope it helps someone..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.