Undoing a Merge in Git with git merge --abort
In the last blog, we saw how to resolve conflicts manually. But what if you start a merge, get conflicts, and then realize — “I don’t want to solve this right now. I just want to go back to the way things were before the merge.”
That’s exactly where git merge --abort helps. Let’s walk through a scenario step by step.
Creating the Scenario
Start with a clean project:
git branch feature2
This creates a new branch feature2. Now stay on main and modify hello.txt:
echo "New line added in main again" >> hello.txt
git add hello.txt
git commit -m "Main branch update"
Switch to feature2 and also modify the same file:
git switch feature2
echo "New line added in feature2" >> hello.txt
git add hello.txt
git commit -m "Feature2 update"
Now both main and feature2 have different commits on the same file.
Triggering the Conflict
Switch back to main and try to merge:
git switch main
git merge feature2
Result:
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
Check the file:
cat hello.txt
Output will show conflict markers:
Hello World
<<<<<<< HEAD
New line added in main again
=======
New line added in feature2
>>>>>>> feature2
Undoing the Merge
If you don’t want to resolve this conflict now, abort the merge:
git merge --abort
This takes you back to the exact state before the merge.
Check hello.txt again:
cat hello.txt
# Hello World
# New line added in main again
Everything is clean again on main, and the conflict is gone.
Why git merge --abort is Important
- Safely reverts back to pre-merge state.
- Useful when you realize mid-merge that something went wrong.
- Prevents committing messy or unintended conflict resolutions.
Summary
- Run
git merge feature2→ conflict occurs. - Instead of resolving, use
git merge --abort. - Git resets you back to the state before the merge.
- Extremely useful for avoiding mistakes in real-world projects.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉