HOWTO · Git
How to Undo a Merge in Git
Choose between git reset and git revert to undo a Git merge, select the right commit and parent, protect local work, and verify the result.
On this page
Choose the undo method based on whether the merge is shared and whether it is still at your branch tip. For an unshared merge, git reset moves the branch back to an earlier commit; --hard also replaces the index and working tree, so it can discard work. For a merge already published or used by others, git revert adds a new commit that reverses the merge’s changes without rewriting shared history. Before either operation, inspect your current branch and protect any uncommitted changes. Avoid force-pushing a reset branch that others use.
Choose how to undo the merge
Use reset when the merge has not been shared and you intend to remove it from the branch history. Use revert when the merge is part of shared history, or when preserving the existing commit graph matters. If later commits depend on the merge, resetting past them removes those commits from the branch too; reverting the merge instead creates a new change and may require resolving conflicts. Neither operation is a substitute for checking what will be changed.
Find the merge commit and its parent
Inspect the graph before choosing a target. git log --oneline --graph --decorate --all shows branch structure; git show --no-patch --pretty=raw <merge> displays a commit’s parent lines. A merge commit has at least two parents. Parent 1 is the tip of the branch checked out when the merge was made, and parent 2 is the merged-in tip; confirm this against the history rather than relying on branch names or a remembered hash.
A small model makes the parent numbering concrete:
history = {
"merge": ["main-before", "feature-tip"],
"main-before": ["base"],
"feature-tip": ["base"],
}
merge = "merge"
print(f"merge: {history[merge][0]} (parent 1), {history[merge][1]} (parent 2)")
The labels are illustrative; use commit IDs from your own repository. This example models the relationship only and does not run Git commands.
Undo an unshared merge with git reset
First check git status and save or commit work you need to keep. If the merge is the current tip and its first parent is the desired pre-merge state, git reset --hard <merge>^1 moves the current branch there and resets the index and working tree. Review the target before running it: --hard discards tracked local changes that differ from that commit. If you want to keep the changes in your working tree, git reset --mixed <merge>^1 moves HEAD and resets the index while leaving the working tree in place. git reset --soft <merge>^1 moves HEAD while leaving the index and working-tree contents unchanged. Because staging is measured against the new HEAD, the staged-versus-unstaged status of those contents may differ after the reset.
HEAD~1 selects the first parent of the current commit. It is appropriate only when HEAD is the merge commit and that first parent is the intended destination. ORIG_HEAD is often set to the prior tip by operations such as merge, but it can later be overwritten; inspect it with git show --no-patch --oneline ORIG_HEAD before using it. git reset --merge <target> can preserve some unstaged local changes while updating the index and working tree, but it may stop when changes conflict with the reset. It is not a general backup. Resetting a published merge rewrites the branch history and can disrupt collaborators.
Undo a shared merge with git revert
For a shared merge, use git revert -m <parent-number> <merge-commit>. The mainline number tells Git which parent history to keep as the baseline while reversing the merge’s changes. Choose it by inspecting the merge’s parent lines and deciding which side should remain; 1 is common when the merge was made on the destination branch, but it is not universal. Revert creates a new commit and leaves the original merge in history.
If Git reports conflicts, resolve the files and stage the resolutions. Before completing the revert, inspect the staged resolution with git diff --cached and confirm it reverses the intended changes. Then run git revert --continue, which creates the revert commit. Use git revert --abort to stop the in-progress revert. Reverting a merge affects how later merges from the same branch are applied: commits already present in the reverted merge are still recorded in ancestry, so restoring those changes later may require reverting the revert or merging new commits.
Verify the result and recover if needed
Afterward, inspect git status, git log --oneline --graph -n 8, and the latest commit’s diff with git show --stat --oneline HEAD. Confirm the branch points where intended, no unexpected local changes were lost, and a revert commit contains the expected reversal. For a reset, the former tip may still be recoverable through the reflog: inspect git reflog and, if the commit is present, create a recovery branch with git branch rescue <commit-id> before further changes. Reflog entries expire and are local, so they are not a substitute for a backup. If a shared branch was reset accidentally, coordinate with collaborators before restoring or publishing history.