redAce
redAce

Reputation: 1798

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

Upvotes: 138

Views: 238965

Answers (5)

Jason Geng
Jason Geng

Reputation: 91

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>

Upvotes: 3

Prasanth Rajendran
Prasanth Rajendran

Reputation: 5512

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.

Upvotes: 22

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39981

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.

Upvotes: 209

poige
poige

Reputation: 1853

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

Upvotes: 7

Porcupine
Porcupine

Reputation: 6475

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

git merge --abort

Upvotes: 102

Related Questions