如何解除 Git 合并?

我不小心从 dev 做了一个 git pull origin master,然后 master 被合并到 dev 中。 有可能解除合并吗?

我已经看到了不同的解决方案,我从开发人员和主人那里尝试了这个方案: git revert -m 1 <commit>(每人一次) 但我得到的是: 一切都是最新的,每一次

181369 次浏览

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.

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

git merge --abort

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

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.

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>