git - 有没有一种方法可以干净轻松地撤消 git merge?

标签 git git-merge git-reset git-revert

我读过执行 git reset commitBeforeDoingMerge --hard 不会撤消 merge :

1pm-3pm This is my branch and I need to merge master into it
1pm-2pm This was master
1pm-2pm-3pm-4pm Merging ended up doing this, 4pm being the merge commit
1pm-2pm-3pm if This is the result if you do git reset 3pm --hard, because it does not undo the merge, it just goes back to that commit in the branch that was merged.

现在,另一个建议是执行 git revert 4pm -m 1,但根据 manual 这有问题:

Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.

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.

那么,有没有一种方法可以干净、轻松地撤消 git merge ?

最佳答案

I've read that doing git reset commitBeforeDoingMerge --hard would no undo the merge

无论你在哪里读到它,它都是错误的。这正是撤消 merge 的方法。

假设您要 merge branch进入master .所以我们在 merge 之前有这个:

A -- B -- C (master)
 \
  X -- Y (branch)

好的,所以我们 git checkout mastergit merge branch .结果是:

A -- B -- C -- D (master)
 \             |
  X -- Y (branch)

...其中 D 是 merge 提交,C 和 Y 是其父级。

如果你现在说 git reset --hard <SHA-of-C> ,你回到了这个:

A -- B -- C (master)
 \
  X -- Y (branch)

这是一个完美的撤销。

关于git - 有没有一种方法可以干净轻松地撤消 git merge?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64796878/

相关文章:

git - 如何减少我的 github 存储库的实际大小? (而不仅仅是我的本地 checkout )

git - 多个差异工具

git - 是否可以在 git 中区分 lfs 文件?

git - 通过portablegit使用github时如何使用指定的 key ?

git - 包含敏感数据的私有(private)和公共(public) Git 存储库

git merge --no-commit 在 IntelliJ IDEA 中运行时创建 merge 提交

git - 是否可以在 Git 中签署 merge 提交?

Git 无法撤销修改过的文件

git - 为什么 git reset commit + git push 会导致当前分支的提示落后于远程?

git - 如何在 'git-pull' 自动 merge 后更改提交消息?