How to undo a git merge with conflicts
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Undoing a merge with conflicts depends on how far the merge got. If the merge is still in progress and has not been committed, the usual answer is git merge --abort. If the merge was already committed, you need a different strategy.
That distinction matters because Git is protecting two different states: an unfinished merge in your working tree, or a merge commit already recorded in history. The right undo command changes with the state.
Case 1: Merge In Progress, Conflicts Unresolved
If you ran git merge, conflicts appeared, and you want to stop and go back to the pre-merge state, use:
This is the normal safe answer for an unfinished conflicted merge. It tells Git to restore the branch to the state it had before the merge started.
Before doing that, inspect the state:
If Git reports that a merge is in progress, git merge --abort is the right first move.
Case 2: Merge Commit Already Created Locally
If you already resolved conflicts and committed the merge, then git merge --abort no longer applies because the merge is finished. If the merge commit is still local and you want to discard it completely, one common option is:
This resets the branch back to where it was before the merge began.
That is destructive for local uncommitted work, so use it only when you are sure you want to throw away the merge result.
Case 3: Merge Already Pushed
If the merge commit has already been pushed and other people may have pulled it, rewriting branch history is usually the wrong move. In that case, revert the merge:
The -m 1 tells Git which parent should be treated as the mainline. This creates a new commit that undoes the merge without rewriting published history.
How To Check Which Situation You Are In
Use these commands:
Interpret them like this:
- merge in progress and uncommitted: abort
- merge committed but local: reset if safe
- merge committed and pushed: revert
That simple decision tree avoids a lot of panic-driven mistakes.
What Happens to Conflict Markers
If you abort an in-progress merge, Git removes the conflict state and restores files as they were before the merge started. If you already committed the merge, conflict markers no longer matter because the merge result is now just ordinary committed content.
So conflict markers themselves are not the issue. The issue is whether the merge is still a temporary state or already part of history.
Common Pitfalls
- Using
git reset --hardwhen the merge is still in progress andgit merge --abortwould have been safer. - Trying
git merge --abortafter the merge was already committed. - Rewriting history on a pushed branch instead of reverting the merge.
- Forgetting to inspect
git statusandgit logbefore choosing the undo strategy. - Losing unrelated local changes because a destructive reset was used casually.
Summary
- If the conflicted merge is still in progress, use
git merge --abort. - If the merge commit exists only locally and you want to discard it,
git reset --hard ORIG_HEADmay be appropriate. - If the merge was already pushed, prefer
git revert -m 1 <merge_commit>. - Always check whether the merge is unfinished, committed, or published before undoing it.
- The correct undo method depends on Git state, not just on the word "conflicts."
Related reading
- How to undo a git pull?
- How to undo git commit --amend done instead of git commit
- How to undo local changes to a specific file
- How to unstage large number of files without deleting the content
- How to unstash only certain files?
- How to update a file in remote repo, without cloning that repo first?
- How to update a git shallow clone?
- How to update a pull request from forked repo?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.