115

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

redAce
  • 1,558
  • 4
  • 14
  • 24

5 Answers5

194

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.

Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • Since it's an odd merge : "pull origin master: Merge made by the 'recursive' strategy" i'm a bit lost. Would you advise me to do a git reset --hard 757501b from master (even though there is no changes in history). Or git reset --hard 14cbb9c from dev ? – redAce Mar 08 '15 at 22:28
  • It's dev you need to fix, right? So find the commit that was made on dev just before this bad merge and do the reset on dev to that commit. – Andreas Wederbrand Mar 09 '15 at 05:15
  • 2
    git revert -m 1 – mold Oct 11 '17 at 00:09
  • 1
    Worst noting that if you pushed a merge commit, then NOT apply this approach, as that will mess up one's master branch who pulled it. Prefer revert instead. – coffman21 Dec 24 '19 at 06:25
  • type :q to exit vim after reflog – josef Jun 17 '21 at 15:32
93

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

git merge --abort

Porcupine
  • 5,885
  • 2
  • 19
  • 28
15

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.

Prasanth Rajendran
  • 4,570
  • 2
  • 42
  • 59
6

git revert -m allows to un-merge still keeping the history of both merge and un-do operation. Might be good for documenting probably.

poige
  • 1,562
  • 15
  • 12
1

If it's a history merge. First to find the merge commit id using git log like this:

 git log --after="2022-09-29 00:00:00" --before="2022-09-30 23:00:00"

This will show the log in 2022-09-29 (suppose the merge happened in 2022-09-29)

Then find the commit id, and run:

git revert -m 1 <commit id>
Jason Geng
  • 59
  • 1
  • 8