How to undo git merge

Undoing a Git merge is necessary when a merge introduces conflicts, breaks functionality, or merges the wrong branches. With over 25 years of software development experience and as the creator of CoreUI, I’ve recovered from problematic merges countless times. Git provides multiple approaches to undo merges depending on whether the merge is completed, pushed, or still in progress. This approach helps you safely revert to pre-merge state without losing work.

Use git reset, git revert, or git merge –abort to undo merges based on merge status and history.

Abort merge in progress:

# You started a merge and got conflicts
git merge feature-branch
# CONFLICT (content): Merge conflict in file.js

# Abort the merge and return to pre-merge state
git merge --abort

# Verify you're back to original state
git status

Undo completed merge (not pushed):

# You just completed a merge
git merge feature-branch
# Merge made by the 'ort' strategy

# Check commit history
git log --oneline

# Undo merge using reset (moves HEAD back)
git reset --hard HEAD~1

# Or use reflog to find pre-merge commit
git reflog
git reset --hard HEAD@{1}

Undo merge with reset (different modes):

# Hard reset - discards all changes
git reset --hard HEAD~1

# Mixed reset - keeps changes unstaged
git reset --mixed HEAD~1

# Soft reset - keeps changes staged
git reset --soft HEAD~1

# After soft reset, you can modify and recommit
git commit -m "Modified merge"

Undo pushed merge with revert:

# You merged and pushed to remote
git merge feature-branch
git push origin main

# Use revert to create new commit that undoes merge
# -m 1 means keep the first parent (main branch)
git revert -m 1 HEAD

# Push the revert
git push origin main

# This creates a new commit, preserving history

Understanding merge parents:

# View merge commit details
git show HEAD

# See parent commits
git log --graph --oneline

# Merge commit has two parents:
# -m 1: keep changes from main branch (first parent)
# -m 2: keep changes from merged branch (second parent)

# Revert keeping main branch changes
git revert -m 1 <merge-commit-hash>

Undo merge and keep working on branch:

# You merged feature into main prematurely
git checkout main
git merge feature
# Realized the merge was wrong

# Undo merge
git reset --hard HEAD~1

# Continue working on feature branch
git checkout feature
# Make more commits

# Merge again when ready
git checkout main
git merge feature

Undo specific merge commit:

# Find the merge commit hash
git log --oneline --graph

# Undo that specific merge
git revert -m 1 abc1234

# Or reset to before that merge
git reset --hard abc1234^

Undo merge with conflicts resolution:

# During merge conflict resolution
git merge feature
# CONFLICT in multiple files

# If you want to abort
git merge --abort

# Or if you resolved conflicts but want to undo
git reset --merge

Recovering after bad merge:

# You merged, made commits, then realized merge was wrong
git log --oneline
# abc1234 Recent work
# def5678 More work
# ghi9012 Merge branch 'feature'
# jkl3456 Previous commit before merge

# Reset to before merge
git reset --hard jkl3456

# If you want to keep work done after merge
git reset --soft jkl3456
# Changes from commits after merge are now staged

Undo merge on shared branch:

# For shared branches, use revert instead of reset
git revert -m 1 <merge-commit>
git push origin main

# Communicate with team about the revert
# They should pull the revert commit

Cherry-pick commits from bad merge:

# You merged but want only some commits
git reset --hard HEAD~1

# Cherry-pick specific commits from the branch
git cherry-pick abc1234
git cherry-pick def5678

# Skip commits you don't want

Undo merge with reflog:

# View reflog to see all HEAD movements
git reflog

# Output shows:
# abc1234 HEAD@{0}: merge feature: Merge made by 'ort' strategy
# def5678 HEAD@{1}: commit: Last commit before merge

# Reset to before merge
git reset --hard HEAD@{1}

# Or use commit hash
git reset --hard def5678

Comparing merge approaches:

# git reset - rewrites history (use before push)
git reset --hard HEAD~1

# git revert - creates new commit (use after push)
git revert -m 1 HEAD

# git merge --abort - cancels in-progress merge
git merge --abort

Undo octopus merge (multiple branches):

# You merged multiple branches
git merge branch1 branch2 branch3

# Undo the same way
git reset --hard HEAD~1

# Or revert if pushed
git revert -m 1 HEAD

Checking what will be undone:

# Before undoing, check what changed
git diff HEAD~1 HEAD

# See commits that will be removed
git log HEAD~1..HEAD

# View merge commit details
git show HEAD

Best Practice Note

Use git merge --abort during conflicts to cancel in-progress merges. Use git reset --hard HEAD~1 to undo local merges before pushing. Use git revert -m 1 for pushed merges to maintain history. The -m 1 flag tells revert which parent to keep (usually the main branch). Always verify you’re on the correct branch before undoing merges. For shared branches, coordinate with team before reverting. Use reflog to find pre-merge state if needed. This is how we handle merge mistakes in CoreUI development—safely undoing problematic merges while preserving important commits and maintaining clean history.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to remove a property from an object in Javascript
How to remove a property from an object in Javascript

How to loop inside React JSX
How to loop inside React JSX

How to Use JavaScript setTimeout()
How to Use JavaScript setTimeout()

How to get element ID in JavaScript
How to get element ID in JavaScript

Answers by CoreUI Core Team