How to undo a merge in GitHub

44    Asked by manish_3307 in Devops , Asked on Aug 16, 2025

How can you undo a merge in GitHub if you merged the wrong branch or introduced unwanted changes? What Git commands or options help you safely roll back to the state before the merge?

Answered by Ramya

Undoing a merge in GitHub (or Git in general) depends on whether the merge has already been pushed to the remote repository or not. Sometimes, you might merge the wrong branch or realize that the changes are not ready, and you’ll want to roll things back safely.

Here are the common ways to undo a merge:

If the Merge is Local (Not Pushed Yet):

  •  You can simply reset your branch back to the commit before the merge.

 git reset --hard HEAD~1

  •  This removes the merge commit and restores your branch to its previous state. Be careful: this will also discard any uncommitted changes.

If the Merge is Already Pushed to GitHub:

  •  In this case, you can’t just reset without force-pushing (which is risky). Instead, use git revert:

 git revert -m 1

  • -m 1 tells Git to keep the mainline branch history intact.
  • This creates a new commit that undoes the merge while preserving history.

Alternative: Force Push (Not Recommended)

  •  You could do a git reset --hard to a previous commit and then force push:

 git push origin HEAD --force

 But this rewrites history, which can cause problems for collaborators.

Best Practice:

 Use git revert for merges that are already on GitHub since it’s safer and keeps history clean. Only use git reset --hard if the merge hasn’t been pushed yet.



Your Answer

Interviews

Parent Categories