Undoing a Merge

Updated · 12 min read
mediumGit 2.53gitundo-mergeorig-headrevert-mergeinterview

Mixing two paints is easy to undo only before you pour them together. Once they are mixed in the bucket and the wall is painted, you can't separate them anymore. You paint over the wall instead.

Undoing a merge in Git follows the same idea. Before the merge is finished, you simply cancel it. After it is committed but only on your computer, you move the branch back to where it was before. After it has been pushed and others have it, you paint over it: a new commit that reverses everything the merge brought in. Each stage has its own command, and the last one has a trap worth knowing about.

In this lesson, you will undo a merge in each of these situations.

The Starting Point

Advitha's receipts feature has just been merged into main:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Add receipt numbers"
echo "Print after payment" >> receipts.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Print a receipt after payment"
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git merge --quiet --no-edit feature/receipts
git log --oneline --graph
Output
*   5e8f1d8 (HEAD -> main) Merge branch 'feature/receipts'
|\
| * 23743f5 (feature/receipts) Print a receipt after payment
| * 6dcd72f Add receipt numbers
* | d9fe232 Add the hostel fee
|/
* 4988b51 Add README and the term fee

Then the school says receipt printing must wait until next term.

Case 1: The Merge Isn't Finished

If the merge stopped with conflicts and you haven't committed it yet, git merge --abort cancels it completely, as you saw in "Resolving Merge Conflicts". This is the simplest case, and the only one where nothing has been recorded.

Case 2: Merged, but Not Pushed

The merge commit exists only on Navika's computer. She can move main back to where it was before the merge. Git remembers that position in ORIG_HEAD, which git merge, git reset and git rebase write before they move a branch:

Terminal
git reset --hard ORIG_HEAD
git log --oneline --graph --all
ls
Output
HEAD is now at d9fe232 Add the hostel fee
* d9fe232 (HEAD -> main) Add the hostel fee
| * 23743f5 (feature/receipts) Print a receipt after payment
| * 6dcd72f Add receipt numbers
|/
* 4988b51 Add README and the term fee
README.md
fees.txt

main is back at "Add the hostel fee", receipts.txt is gone from the folder, and the merge commit is no longer on any branch. Advitha's branch is untouched, so the feature can be merged again whenever it is ready.

Why ORIG_HEAD and Not HEAD~1?

After a merge commit, HEAD~1 happens to be the right commit too, because the first parent of a merge is the previous main. But after a fast-forward merge, there is no merge commit, and HEAD~1 steps back only one of the merged commits. ORIG_HEAD is right in both cases:

Terminal
git switch --quiet -c feature/late-fee
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
echo "Charged after the 10th" >> fees.txt
git commit --quiet -am "Say when the late fee applies"
git switch --quiet main
git merge --quiet feature/late-fee
git log --oneline -3
git reset --hard ORIG_HEAD
git log --oneline -1
Output
9b4afc3 (HEAD -> main, feature/late-fee) Say when the late fee applies
65ad233 Add the late fee
d9fe232 Add the hostel fee
HEAD is now at d9fe232 Add the hostel fee
d9fe232 (HEAD -> main) Add the hostel fee

The fast-forward brought two commits, and ORIG_HEAD took both back out of main.

Case 3: Merged and Pushed

Now Navika merges the receipts feature again, and this time she pushes it. Teammates pull it. Resetting is no longer an option, because it would rewrite history others already have. The answer is git revert, but a merge commit needs one extra piece of information:

Terminal
git merge --quiet --no-edit feature/receipts
git revert --no-edit HEAD
Output
error: commit 3a2e6e32eabbdd447c1ac9e82f18c57e644bc581 is a merge but no -m option was given.
fatal: revert failed

A merge commit has two parents, and the "opposite" of a merge depends on which side you want to keep. -m 1 (mainline 1) says: keep parent 1, the branch that was merged into, and undo what the other side brought in:

Terminal
git revert -m 1 --no-edit HEAD
git log --oneline --graph -4
ls
Output
[main b68a92a] Revert "Merge branch 'feature/receipts'"
 Date: Mon Jan 5 10:38:00 2026 -0600
 1 file changed, 2 deletions(-)
 delete mode 100644 receipts.txt
* b68a92a (HEAD -> main) Revert "Merge branch 'feature/receipts'"
*   3a2e6e3 Merge branch 'feature/receipts'
|\
| * 23743f5 (feature/receipts) Print a receipt after payment
| * 6dcd72f Add receipt numbers
README.md
fees.txt

The revert commit removed receipts.txt, everything the feature added, and kept the hostel fee and everything else on main. The merge stays in the history, and the revert sits on top of it, so everyone's copies still agree.

The Trap: Merging the Branch Again

Next term, Advitha adds one more commit to the feature, and Navika merges the branch again, expecting the whole receipts feature to come back:

Terminal
git switch --quiet feature/receipts
echo "A5 paper" > receipt-format.txt
git add receipt-format.txt
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Set the receipt paper size"
git switch --quiet main
git merge --quiet --no-edit feature/receipts
ls
Output
README.md
fees.txt
receipt-format.txt

Only receipt-format.txt arrived. receipts.txt is still missing. Git sees that Advitha's first two commits are already merged: they are in main's history, through the first merge. So it brings only the new commit. The revert that removed their changes is also in main's history, and nothing undoes it.

The fix is to revert the revert. That brings back everything the first revert removed:

Terminal
git revert --no-edit ":/Revert \"Merge branch"
ls
git log --oneline -3
Output
[main 7122068] Reapply "Merge branch 'feature/receipts'"
 Date: Mon Jan 5 10:50:00 2026 -0600
 1 file changed, 2 insertions(+)
 create mode 100644 receipts.txt
README.md
fees.txt
receipt-format.txt
receipts.txt
7122068 (HEAD -> main) Reapply "Merge branch 'feature/receipts'"
c6f9a68 Merge branch 'feature/receipts'
425c141 (feature/receipts) Set the receipt paper size

":/Revert \"Merge branch" names the newest commit whose message contains that text, which is the earlier revert, so you don't have to copy its ID. All of the feature is back: receipts.txt and receipt-format.txt.

Syntax

SituationCommand
Conflicts, not committedgit merge --abort
Merge committed, not pushedgit reset --hard ORIG_HEAD
Same, later ongit reflog, then git reset --hard <old position>
Merge pushedgit revert -m 1 <merge-commit>
Bringing a reverted branch backgit revert <revert-commit>, then merge again
Choose the undo by where the merge is

Common Mistakes

Using git reset on a pushed merge. Teammates' copies then disagree with yours. Use git revert -m 1.

Forgetting -m when reverting a merge. Git refuses with is a merge but no -m option was given. Add -m 1.

Using -m 2 by accident. That keeps the merged branch's side and undoes main's changes instead. Check the parent order with git log -1 --format=%P <merge> or git show <merge> if in doubt: parent 1 is listed first.

Expecting a second merge to bring back reverted work. Only new commits come in. Revert the revert first.

Using HEAD~1 to undo a fast-forward merge. It undoes only one commit. Use ORIG_HEAD or the reflog.

Under the Hood

ORIG_HEAD is a file like HEAD, in .git/ORIG_HEAD, holding a commit ID. Commands that can move a branch a long way write the old position there first, as a safety net:

Terminal
cat .git/ORIG_HEAD
git log --oneline -1 ORIG_HEAD
Output
b68a92adff226414a226d056a8fc83bc54b58079
b68a92a Revert "Merge branch 'feature/receipts'"

Here it holds b68a92a, the revert commit: where main was just before the second merge of feature/receipts.

A merge commit's revert with -m 1 is calculated like any other revert: Git takes the difference between the merge and its first parent, which is exactly what the merge brought into main, and applies the opposite. The merge commit itself stays, and so does its claim that the feature's commits are part of main's history. That claim is the reason for the trap above: Git decides what to merge from history, not from the files, and in history those commits are already merged.

Interview Corner

How do you undo a merge that you haven't pushed?

git reset --hard ORIG_HEAD right after the merge, or git reset --hard <commit> using the position from git reflog. It works for merge commits and fast-forwards. It throws away uncommitted changes, so check git status first.

How do you undo a merge that is already pushed?

git revert -m 1 <merge-commit>. It creates a new commit that reverses the changes the merge brought in, relative to the first parent (the branch that was merged into), without rewriting history.

What does -m 1 mean in git revert -m 1?

"Mainline parent 1": treat the merge's first parent as the version to keep. The revert then undoes the difference between that parent and the merge, which is the work from the merged branch. -m 2 would keep the second parent's side instead.

You reverted a merge. Later the branch is merged again, but its old changes don't come back. Why, and how do you fix it?

The old commits are already in the target branch's history, so Git merges only the new commits, and the earlier revert still undoes the old ones. Revert the revert commit (before or after the new merge) to bring the old changes back.

What is ORIG_HEAD?

A reference that git merge, git reset, git rebase and a few other commands set to the previous position of HEAD before they move it. It allows a quick undo, such as git reset --hard ORIG_HEAD.

Practice

Challenge: the wrong undo. In the same repository, create feature/exam-fee with two commits that each add a line to a new file exam.txt, and fast-forward main to it. Then "undo" the merge with git reset --hard HEAD~1 instead of ORIG_HEAD, and print the one-line log of the three newest commits.

What does Git print? Before you open the solution, decide which of the two exam fee commits is still on main.

Show solution
Terminal
git switch --quiet -c feature/exam-fee
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git commit --quiet -m "Add the exam fee"
echo "Paid once a year" >> exam.txt
git commit --quiet -am "Explain when the exam fee is paid"
git switch --quiet main
git merge --quiet feature/exam-fee
git reset --quiet --hard HEAD~1
git log --oneline -3
Output
b3586c5 (HEAD -> main) Add the exam fee
7122068 Reapply "Merge branch 'feature/receipts'"
c6f9a68 Merge branch 'feature/receipts'

The first exam fee commit, "Add the exam fee", is still on main. The merge was a fast-forward of two commits, and HEAD~1 stepped back over only one of them.

ORIG_HEAD can't rescue you now, because git reset itself just overwrote it with the position from before the reset, the second exam fee commit. The reliable way back is git reflog: find the entry from before the merge feature/exam-fee line, and git reset --hard to it.

Checkpoint. In a practice repository, merge a feature branch, undo it with ORIG_HEAD, and merge it again. Then pretend it was pushed: revert it with -m 1, add a commit to the feature, merge it again, and bring the whole feature back by reverting the revert.

Quick check: Undoing a merge

4 questions
1You merged a branch locally and haven't pushed. What undoes it?
2Why does git revert HEAD fail on a merge commit?
3After reverting a merge, you merge the same branch again. What comes in?
4Which command undoes both commits of a fast-forward merge?

Key Takeaways

Quick reference

CommandIt is used to
git merge --abortCancel a merge with conflicts
git reset --hard ORIG_HEADUndo a merge that isn't pushed
git revert -m 1 <merge>Undo a pushed merge with a new commit
git revert <revert-commit>Bring back changes removed by a revert
git log -1 --format=%P <merge>Show a merge's parents, in order
git reflogFind where a branch was before a merge or reset

Was this tutorial helpful?

Lesson 33 of 106