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 git@xxx.beanstalkapp.com:/yyy.git
 ! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@xxx.beanstalkapp.com:/yyy.git'

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

Will
  • 5,370
  • 9
  • 35
  • 48
  • 1
    Does [this](http://superuser.com/a/35272/53715) help you? –  Aug 06 '12 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 06 '12 at 14:16
  • If you've already rolled back the commits on the git server, then why can't you pull? – invalidsyntax Aug 06 '12 at 14:19
  • possible duplicate of [Git reset --hard and a remote repository](http://stackoverflow.com/questions/1377845/git-reset-hard-and-a-remote-repository) – tripleee Aug 06 '12 at 16:16

3 Answers3

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
vergenzt
  • 9,669
  • 4
  • 40
  • 47
3

Does your server disallow non-fast forward push?

git config file

[receive]
denyNonFastforwards = true
linquize
  • 19,828
  • 10
  • 59
  • 83
  • 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 06 '12 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 '13 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..

Karthik Bose
  • 33,556
  • 3
  • 33
  • 43