How To Revert A Merge In Git

5 min read

How to Revert a Merge in Git: A Complete Guide for Every Scenario

Merging branches is one of the most common operations in Git, but sooner or later every developer faces a situation where a merge needs to be undone. On the flip side, maybe a feature was merged prematurely, a bug slipped into the main branch, or a merge produced unexpected results. Knowing how to revert a merge in git correctly is a critical skill that separates confident developers from those who panic and reach for risky commands. This guide walks you through every reliable method, explains what happens under the hood, and helps you choose the safest approach for your specific situation Worth keeping that in mind..

Introduction: Why Reverting a Merge Is Different

Reverting a normal commit is straightforward. Which means reverting a merge is trickier because a merge commit is not an ordinary commit. It has two or more parent commits, which means Git needs to know which side of the merge you want to keep before it can undo the changes.

This is where a lot of people lose the thread Easy to understand, harder to ignore..

Choosing the wrong method can rewrite shared history, confuse your teammates, or make it nearly impossible to re-merge a branch later. That is why understanding the why behind each command matters just as much as memorizing the command itself.

Before diving in, always ask yourself one key question: has the merge already been pushed and shared with others? Your answer determines which method is safe to use.

Understanding Merge Commits and Parent Lines

When you run git merge, Git creates a merge commit that ties two histories together. This commit has multiple parents:

  • Parent 1 (the mainline): the branch you were on when you ran the merge, usually main or master.
  • Parent 2: the branch you merged into your current branch, such as a feature branch.

This parent numbering is the heart of reverting a merge. When you tell Git to revert a merge commit, you must specify which parent represents the mainline using the -m flag. Get this wrong, and Git will undo the wrong set of changes.

You can always inspect a merge commit's parents by running:

git log --pretty=format:'%h %p %s' -n 1

The first hash is the commit itself; the following hashes are its parents, listed in order.

Method 1: Revert a Merge That Has Been Pushed (Recommended)

If the merge is already on a shared branch like main, do not rewrite history. Rewriting published commits forces everyone else to fix their local branches and can cause data loss. Instead, use git revert, which creates a new commit that undoes the changes of the merge.

The command looks like this:

git revert -m 1 

Here is what each part means:

  • git revert creates a new commit that reverses the changes.
  • -m 1 tells Git that parent number 1 is the mainline you want to keep.
  • <merge-commit-hash> is the specific merge commit you want to undo.

After running the command, Git opens your editor so you can adjust the commit message. Once saved, the merge is safely undone without altering any existing history. You then push the new commit:

git push origin main

This is the safest and most widely recommended approach for collaborative work because it preserves the complete history and is transparent to your team.

Method 2: Undo a Local Merge That Has Not Been Pushed

If the merge exists only on your machine and has not been shared, you have more freedom. You can simply move your branch pointer back to the commit that existed before the merge.

Git conveniently stores the pre-merge position in a special reference called ORIG_HEAD. To reset to it:

git reset --hard ORIG_HEAD

Alternatively, if you know the merge was the most recent commit, you can step back one commit:

git reset --hard HEAD~1

A serious warning: git reset --hard permanently discards changes. Any uncommitted work or local commits made after the merge will be lost. Use this method only when you are certain the merge has not been pushed and you do not need the discarded changes. When in doubt, create a backup branch first:

git branch backup-before-reset

Method 3: Abort a Merge That Is Still in Progress

Sometimes you start a merge, hit a wall of merge conflicts, and decide the whole thing was a mistake. You do not need to revert anything here because the merge was never completed. Simply abort it:

git merge --abort

This restores your working directory to the exact state it was in before you started the merge. It is clean, instant, and completely safe. If for some reason --abort does not work, you can fall back to:

git reset --merge

The "Revert of a Revert" Problem

Here is a subtle trap that catches even experienced developers. Now, suppose you reverted a merge with git revert -m 1. Later, you fix the underlying issues and want to merge that same branch again. That said, you run the merge, but nothing happens. Git sees that the branch's commits are already part of your history and assumes there is nothing new to bring in.

The solution is to revert the revert. Put another way, you undo the commit that undid the merge:

git revert 

This re-applies the changes from the original branch. Keep in mind that this brings back all the original changes, so if you only wanted a portion of them, you may need to make additional edits afterward That's the part that actually makes a difference..

What Happens Under the Hood

When you run git revert -m 1, Git performs a three-way comparison. It compares the merge commit against the mainline parent you selected, calculates the difference, and applies the reverse of that difference as a new commit. Your original history remains untouched; Git simply layers a new commit on top that cancels out the merge's effect And it works..

Easier said than done, but still worth knowing.

When you use git reset --hard, Git instead

Up Next

Recently Launched

Explore the Theme

Before You Head Out

Thank you for reading about How To Revert A Merge In Git. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home