Recovery
Recover after an accidental merge
When the wrong branch was merged, decide between reset and revert -m based on sharing state, and undo the merge safely without damaging collaboration history.
- Anyone actively handling a Git mistake
- Readers who want a conservative rescue habit before trouble happens
- Stop mutating the repo further
- Be ready to inspect `git reflog`, `git status`, and `git log --graph`
- Running more reset or rebase commands before preserving a checkpoint
- Changing shared history before assessing blast radius
Citations & Further Reading
- Git merge [Official]
- Git revert [Official]
- Git reset [Official]
What you will learn
- Understand the core purpose of Recover after an accidental merge
- Master the basic usage and common options of Recover after an accidental merge
- When the wrong branch was merged, decide between reset and revert -m based on sharing state, and undo the merge safely without damaging collaboration history.
- Understand key concepts: Key checks
- Know when to use this feature and when to avoid it
Most merge-recovery mistakes happen when teams use reset where revert -m is required.
Start with a problem
You just ran a Git command and the result wasn't what you expected — maybe you even lost some commits. This has happened before, and you want a reliable set of recovery techniques.
Key checks
- Has the merge commit been pushed?
- Has anyone branched from it?
Merge timePushed or notAffected file count
git reset --mergegit revertgit reflog
If already pushed to a shared branch, do not reset; use revert instead.
First inspection
git log --oneline --graph --decorate -n 20
git branch -vv
git reflog
Recovery options
Merge not shared
git reset --hard HEAD~1
Merge already shared
git revert -m 1 <merge-commit>
-m 1 keeps the first-parent perspective.
Choosing the wrong parent can revert the wrong side of history and create additional recovery work.
Good follow-up reads
Try it yourself
- Practice the recover-after-accidental-merge command in a test repository and observe state changes before and after
- Experiment with different options and compare the output differences
- Simulate a real scenario where you would need to use this, and walk through the full process
Further reading
Keep going on the same topic: