Can not push changes after using git reset --hard

GitGit Revert

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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)

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.

Solution 2 - Git

You need to specify what ref you want to push:

git push -f origin MyBranch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmansView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitralphtheninjaView Answer on Stackoverflow