Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unmerge a Git merge?

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

like image 668
redAce Avatar asked Mar 08 '15 22:03

redAce


People also ask

How do I Unmerge a merge?

Select the Merge & Center down arrow. Select Unmerge Cells.

How do I Unmerge a merged branch?

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

Is it possible to Unmerge in git?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.


2 Answers

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.

like image 177
Andreas Wederbrand Avatar answered Oct 16 '22 18:10

Andreas Wederbrand


If you haven't committed the merge, then use:

git merge --abort

like image 31
Porcupine Avatar answered Oct 16 '22 18:10

Porcupine