142

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

2
  • I merged the wrong branch but didn't push to remote. Why isn't there a git merge undo? Commented Nov 5, 2024 at 20:15
  • If git is supposed to be a version control then shouldn't there be a version history and undo list? See Photoshop History panel. It has undo and redo Commented Nov 5, 2024 at 20:18

5 Answers 5

217

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 ?
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.
git revert -m 1 <commit id>
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.
type :q to exit vim after reflog
108

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

git merge --abort

4 Comments

Thanks it worked because I have yet to commit the merge :)
@Eightgate, he has mentioned explicitly "If you haven't committed the merge, then use: git merge --abort"
This will work if you haven't committed the merge, cheers.
I was eating potato chips and accidently merged dev instead of main. When I run this command I get, fatal: There is no merge to abort (MERGE_HEAD missing).
28

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.

2 Comments

I tried the other options here and got errors. I mentioned them in the comments. I tried this git reset --merge HEAD~1 and there were no errors. The console did something and vscode showed some git activity and then returned to the prompt. ...I think it worked. Source control graph shows some changes but no merged branches... going to try incorrect branch merge again to see... merged in incorrect branch and it shows up in source control branch (merged incorrect branch). ran the command again and it undoes it.
In my case, I hadn't pushed the changes (or synced). The branch was merged locally but nothing else. Running this command undid that change. The other answers might work if someone had already pushed / synced the merged branch changes.
7

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

3 Comments

Be careful with this one as it says here, Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.
for me error: switch m' requires a value`
@1.21gigawatts you need to specify which of the commit of the merge you want to revert (1st or 2nd)
3

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.