Undo a git merge that has been pushed to the server
Just learned that when you accidentally merge on the wrong branch and push, there is a way to change it.
git reset --hard [sha-commit-before-merge]
git push [origin] [branch] --force
This will undo the merge and any commits after the merge, so this is useful if you catch your mistake before any more commits happens.
Related protips:
Written by Matthew Harmon
Related protips
3 Responses
It is better to do git revert -m 1 commit_hash
when the branch has already been pushed or shared, as it won't cause a rewriting of the history, and doesn't require --force.
What excalq said... It's a bad idea to rewrite the history of any pushed branch unless you can be absolutely confident that nobody else has pulled or fetched in the meanwhile.
Better not..
if no new commit happens, but someone fetched master, your commit will soon be added back...
--force, unless used on an isolated branch with a single comitter, should be avoided.