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?
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, andgit 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
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
If the OP didn’t have access to the repo, he/she would have to:
git reset --hard?"):git reset HEAD@{1}git revert:git revert -m 1 HEAD~(in your case)By example:
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.