Revert a merge after being pushed
The first option is the use of git revert
.
git revert -m 1 [sha-commit-before-merge]
The git revert
will revert the changes but will keep the history. Therefore you will not be able to continue working in the same branch since you cannot see the actual difference between the merged branch and your feature branch anymore.
Use the following way to remove history as well. Do this very carefully if and only if you are the only one pushing changes to the branch at the moment.
git reset --hard [sha-commit-before-merge]
git push [origin] [branch] --force
You can revert the merge following the official guide, however this leaves Git with the erroneous belief that the merged commits are still on the target branch.
Basically you have to :
git revert -m 1 (Commit id of the merge commit)
Try using git reflog <branch>
to find out where your branch was before the merge and git reset --hard <commit number>
to restore the old revision.
Reflog will show you older states of the branch, so you can return it to any change set you like.
Make sure you are in correct branch when you use git reset
To change remote repository history, you can do git push -f
, however this is not recommended because someone can alredy have downloaded changes, pushed by you.