o0krbw
Last Updated: February 25, 2016
·
27.42K
· mattdharmon
De056d87a99621dae0f3ec4f33f2c8e2

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.

Say Thanks
Respond

3 Responses
Add your response

14769
D13dff31735876d38f7e7a6e2ea10202

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 ·
17951
02688ffd42b4265a4fa2ca1683115e4e

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 ·
17955

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 ·
Filed Under