Undo a Git merge that hasn't been pushed yet
Categories:
How to Undo a Git Merge Before Pushing to a Remote Repository

Learn various strategies to revert a Git merge that has not yet been pushed to a shared remote repository, preserving your commit history or cleaning it up as needed.
Merging branches in Git is a common operation, but sometimes you might merge the wrong branch, merge too early, or introduce unintended changes. If this merge hasn't been pushed to a remote repository yet, you have several options to undo it without affecting others. This article will guide you through the most common and effective methods, explaining when to use each one.
Understanding Your Options: Revert vs. Reset
Before diving into the commands, it's crucial to understand the difference between git revert
and git reset
. Both can undo changes, but they do so in fundamentally different ways, impacting your commit history. Since we're dealing with a merge that hasn't been pushed, we have the flexibility to use git reset
, which rewrites history. If the merge had been pushed, git revert
would be the safer choice to avoid disrupting collaborators' work.
flowchart TD A[Start: Merge Occurs] --> B{Merge Pushed?} B -- No --> C[Undo Options: Reset (Hard/Soft/Mixed)] B -- Yes --> D[Undo Options: Revert] C --> E[History Rewritten] D --> F[New Commit Reverts Changes] E & F --> G[End: Merge Undone]
Decision flow for undoing a Git merge.
Method 1: Using git reset --hard
(Cleanest History)
The git reset --hard
command is the most straightforward way to completely undo a merge and discard all changes introduced by it. This method rewrites your local history, effectively making it as if the merge never happened. It's ideal when you want a clean slate and are certain you don't need any of the changes from the merge commit or subsequent work.
git reset --hard
will discard all uncommitted changes in your working directory and staging area. Ensure you have stashed or committed any work you wish to keep before executing this command.1. Step 1: Identify the commit before the merge
Use git log
to find the commit hash of the commit just before the merge commit. This is the commit you want to reset to.
2. Step 2: Execute the hard reset
Run git reset --hard <commit-hash>
where <commit-hash>
is the hash you identified in the previous step. This will move your HEAD
and current branch pointer to that commit, and reset your working directory and staging area to match it.
3. Step 3: Verify the reset
Use git log
again to confirm that the merge commit and any subsequent commits are no longer in your history. Your working directory should also reflect the state of the chosen commit.
git log --oneline --graph
# Look for the commit hash before your merge commit
git reset --hard HEAD~1
# Or, if you know the exact commit hash:
git reset --hard <commit-hash-before-merge>
Example of using git log
and git reset --hard
.
Method 2: Using git reset --soft
(Preserve Changes)
If you want to undo the merge commit but keep the changes introduced by the merged branch in your staging area, git reset --soft
is the command to use. This is useful if you want to re-evaluate the merge, make adjustments, and then commit the changes again, possibly with a different merge strategy or after resolving conflicts differently.
1. Step 1: Identify the commit before the merge
Similar to the hard reset, use git log
to find the commit hash of the commit before the merge commit.
2. Step 2: Execute the soft reset
Run git reset --soft <commit-hash>
. This will move your HEAD
and current branch pointer to the specified commit, but it will leave your working directory and staging area untouched. All changes from the merge will be staged.
3. Step 3: Re-evaluate and recommit (optional)
At this point, you can inspect the staged changes, make any necessary modifications, and then create a new commit or perform the merge again with a different approach.
git log --oneline --graph
# Find the commit hash before your merge commit
git reset --soft HEAD~1
# Or, if you know the exact commit hash:
git reset --soft <commit-hash-before-merge>
git status
# You should see all changes from the merge as staged
# Now you can modify, unstage, or commit them again
Example of using git reset --soft
to undo a merge while preserving changes.
Method 3: Using git reset --merge
(Specific to Merge Conflicts)
If you were in the middle of a merge and encountered conflicts, but then decided you want to abort the merge entirely, git reset --merge
(or simply git merge --abort
) is the command to use. This command is specifically designed to clean up the state after a failed or interrupted merge, restoring your branch to its state before the merge attempt.
git merge --abort
# This is equivalent to:
git reset --merge
Aborting a merge with conflicts.
git reset
operation. A quick git status
or git branch
can confirm your current location.