97

I had a mistake and commit some changes to git which I should not have committed. After I made the commit, I pushed my changes. I then used the following commands to try and reset my changes.

 git reset --hard head

Now I want to push this 'reset' to the remote repository with this command:

git push MyBranch

But I am getting this error:

remote: error: denying non-fast-forward refs/heads/branch (you should pull first)

I tried to use this command without any success:

git push -f "origin" 

Any idea what I can do?

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
mans
  • 17,104
  • 45
  • 172
  • 321

2 Answers2

170
git push -f origin myBranch 

should work (provided you are aware this can be dangerous if MyBranch was already fetched by others in their own repo)

Since 2012, you also have:

  • git push --force-with-lease (Git 1.8.5+ Q3 2013) which is safer, and
  • git push --force-if-includes (Git 2.30+, Q1 2021), which attempts to ensure that what is being force-pushed was created after examining the commit at the tip of the remote ref that is about to be force-replaced.

Note: if your remote repo ('origin') has its config set with

receive.denyNonFastForwards true

it will deny any non fast-forward push (even when forced).
See "Is there a way to configure git repository to reject 'git push --force'?".


The OP user654019 reports

I managed to solve the problem this time by setting denyNonFastForwards to false and using -f to force the push

If the OP didn't have access to the repo, he/she would have to:

By example:

$ git revert -m 1 [sha_of_C8]
Finished one revert.
[master 88edd6d] Revert "Merge branch 'jk/post-checkout'"
 1 files changed, 0 insertions(+), 2 deletions(-)

revert a merge

A complete discussion on how to revert a merge can be found here.

The idea remains to generate only new commits, including one reverting the changes introduced by the merge commit.
You then can push that new commit, as a fast-forward change.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It did not work. Probably the config is set in repository. Two quesytion: What is the best way to solve this problem if the denyNonFastForwards is true? How can I revert some mchanges (a merge) and create a new commit? – mans Mar 21 '12 at 12:31
  • @user654019: by creating a new commit composed of changes cancelling your previous commit: use [git revert](http://linux.die.net/man/1/git-revert): you will then be able to push that new commit as a fast-forward push. – VonC Mar 21 '12 at 12:34
  • The original commit was a merge and when I tried to do a revert -m 1 I got some error – mans Mar 21 '12 at 12:37
  • similar to this one it was about not able to do a fast forward – mans Mar 21 '12 at 12:53
  • @user654019: did you first restore your HEAD? Because the revert itself should work (as described in http://progit.org/2010/03/02/undoing-merges.html). – VonC Mar 21 '12 at 13:04
  • Thanks, I managed to solve the problem this time by setting denyNonFastForwards to false and using -f to force the push. I would use the solutions presented in VonC commnet next tim. – mans Mar 21 '12 at 13:41
  • @user654019: thank you for your feedback. I have included it in the answer (as well as documentation about the `git revert`) for more visibility. – VonC Mar 21 '12 at 13:51
  • Don't forget the `--` shortcut. This lets you use `git push -f --` pretty quickly for the same effect. – bitfed Nov 10 '13 at 16:59
  • this does not work for me. i tried `git config --get receive.denyNonFastForwards` and it displayed no output, does that mean the option is not set? yet it refuses to let me push "error: denying non-fast-forward refs/heads/master (you should pull first)" but if I pull like it tells me to, eliminate the conflict, then push again, it rejects my changes "because the tip of your current branch is behind" and tell me to pull again, but if I try that it refuses with "Pull is not possible because you have unmerged files". I feel like everything I do is making this WORSE for what should have taken 5s – Michael Sep 05 '17 at 18:42
  • @Michael One: yes, two, this is a config set on the remote side, not on the client side from which you are pushing. – VonC Sep 05 '17 at 18:44
  • @Michael It is best to ask a new question, detailing the exact commands and git status illustrating the state you are in, as well as the OS and git version. That way, I or other contributors can explain to you (and, more importantly, other users who might end up in the same situation) why it took a bit more than 5 seconds – VonC Sep 05 '17 at 18:46
  • @VonC thanks, can you point me to some documentation on how to get and set the remote config? I'm not finding it, and my naive attempts (.e.g "git remote config --get receive.denyNonFastForwards") aren't working. – Michael Sep 05 '17 at 18:47
  • @Michael the git config is simply to be executed on the remote server, which depends on its nature (you wouldn't be able to set it if the remote server is github.com for instance). Again, a detailed question with all the details would be helpful, to you and others. – VonC Sep 05 '17 at 18:49
  • Never mind, i just nuked the whole thing and re-cloned it from scratch. The amount of time spent going down this rabbit hole is ridiculous. – Michael Sep 05 '17 at 18:51
  • 1
    @Michael I understand your frustration. Not a bad solution actually, according to xkcd ;) https://xkcd.com/1597/ – VonC Sep 05 '17 at 18:52
25

You need to specify what ref you want to push:

git push -f origin MyBranch
rtn
  • 127,556
  • 20
  • 111
  • 121