Last Updated: October 04, 2020
·
57.94K
· mattdharmon

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:

Remove all your local git branches but keep master

3 Responses
Add your response

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.

over 1 year ago ·

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.

over 1 year ago ·

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.

over 1 year ago ·