🔄 How to Undo an Accidental Merge to the Master Branch
Merging the wrong code into the master branch is something that happens.Fortunately, Git and ways to undo this safely.
Here’s a guide to help you revert that accidental merge depending on your situation.
💬 Option 1: Revert the Merge Commit (Safe and Recommended)
If the merge created a merge commit, you can revert it with:
git revert -m 1
git push origin master -m 1
tells Git to keep the first parent of the merge (usually master) and revert the changes from the merged branch.
You can find the using git log
This will create a new commit that undoes the changes from the merge — safe and clean.
💥Option 2: Hard Reset (Dangerous on Shared Repos)
If you prefer to reset master to a previous commit (for example, before the merge), you can run:
git reset --hard
git push origin master --force
💥Warning: This method rewrites history and can break things for teammates.
⬅️ Option 3: Use GitLab’s “Revert” Button (Easiest)
If you’re using GitLab and the merge request is still visible:
Go to the merged Merge Request and click the Revert.
GitLab will create a new merge request that reverts the merge.
Merge it — and you're done!
This is a safe and GUI-friendly option if you prefer not to use the terminal.
Top comments (0)