How to unmerge a Git merge?
GitGithubMergeGit PullGit Problem Overview
I accidentally did a git pull origin master
from dev, and master got merged into dev.
Is it possible to unmerge?
I've already seen different solutions, i tried this one from both dev and master :
git revert -m 1 <commit>
(once each)
But i got : Everything is up-to-date, each time
Git Solutions
Solution 1 - Git
You can reset your branch to the state it was in just before the merge if you find the commit it was on then.
One way is to use git reflog
, it will list all the HEADs you've had.
I find that git reflog --relative-date
is very useful as it shows how long ago each change happened.
Once you find that commit just do a git reset --hard <commit id>
and your branch will be as it was before.
If you have SourceTree, you can look up the <commit id>
there if git reflog
is too overwhelming.
Solution 2 - Git
If you haven't committed the merge, then use:
Solution 3 - Git
If the merge has been accepted accidentally by git merge --continue
or if the changes are auto committed when git pull <branch>
, then we can revert or undo the very recent merge by executing
git reset --merge HEAD~1
> This command reverts our repository to the last commit. HEAD refers to the current state of your repository; HEAD~1 is the last commit in your repository.
Solution 4 - Git
git revert -m
allows to un-merge still keeping the history of both merge and un-do operation. Might be good for documenting probably.