如何还原从一个分支合并的所有提交
How to revert all commits merged from one branch
我有一个发布分支,例如 release/4.0.1
,我们曾经将所有更改添加到此版本并将其合并到我们的 QA
分支。我们进行了几次提交并多次合并到我们的 QA
分支。现在,我们要删除 QA
中从 release/4.0.1
进行的所有更改。在分支上完成了其他合并和提交。
最简单的方法是什么?仅还原与该分支相关的更改和提交。
让我们想象一下您的场景:
o---o---o------o (some-other-branch)
\
o---o---o---M1---N---M2 (QA)
\ / /
A---B---C----D (release/4.0.1)
为了撤消 QA
中来自提交 A
、B
、C
和 D
的更改,您需要还原合并提交 M1
和 M2
.
首先,您需要找到 release/4.0.1
之后 QA
上发生的合并提交 forked:
git log --format='%h %s' --right-only --merges releases/4.0.1...QA
这将 return M1
、N
和 M2
1 的 ID 和提交消息。例如:
18abe3e Merge branch 'release/4.0.1'
2db6c39 Merge branch 'some-other-branch'
b49ea3f Merge branch 'release/4.0.1'
此时,您可以从 QA
中的 release/4.0.1
恢复合并提交,从最新的开始:
git switch QA
git revert -m 1 b49ea3f # Reverts M2
git revert -m 1 18abe3e # Reverts M1
或者,您可以在一次提交中还原所有合并提交:
git switch QA
git revert -m 1 --no-commit b49ea3f # Reverts M2
git revert -m 1 --no-commit 18abe3e # Reverts M1
# Verify that everything works
git commit -m "Revert merge commits M2 and M1"
请注意:还原合并提交会删除更改,但不会更改分支的历史记录。如果您想再次将 release/4.0.1
合并到 QA
,这会使事情变得复杂。阅读文档以获取有关 the consequences of reverting a merge.
的更多信息
- 不幸的是,无法过滤掉发生在 之间的其他分支的合并提交。在这个例子中,
N
发生在 M1
和 M2
之间,所以它会被剪掉。
我有一个发布分支,例如 release/4.0.1
,我们曾经将所有更改添加到此版本并将其合并到我们的 QA
分支。我们进行了几次提交并多次合并到我们的 QA
分支。现在,我们要删除 QA
中从 release/4.0.1
进行的所有更改。在分支上完成了其他合并和提交。
最简单的方法是什么?仅还原与该分支相关的更改和提交。
让我们想象一下您的场景:
o---o---o------o (some-other-branch)
\
o---o---o---M1---N---M2 (QA)
\ / /
A---B---C----D (release/4.0.1)
为了撤消 QA
中来自提交 A
、B
、C
和 D
的更改,您需要还原合并提交 M1
和 M2
.
首先,您需要找到 release/4.0.1
之后 QA
上发生的合并提交 forked:
git log --format='%h %s' --right-only --merges releases/4.0.1...QA
这将 return M1
、N
和 M2
1 的 ID 和提交消息。例如:
18abe3e Merge branch 'release/4.0.1'
2db6c39 Merge branch 'some-other-branch'
b49ea3f Merge branch 'release/4.0.1'
此时,您可以从 QA
中的 release/4.0.1
恢复合并提交,从最新的开始:
git switch QA
git revert -m 1 b49ea3f # Reverts M2
git revert -m 1 18abe3e # Reverts M1
或者,您可以在一次提交中还原所有合并提交:
git switch QA
git revert -m 1 --no-commit b49ea3f # Reverts M2
git revert -m 1 --no-commit 18abe3e # Reverts M1
# Verify that everything works
git commit -m "Revert merge commits M2 and M1"
请注意:还原合并提交会删除更改,但不会更改分支的历史记录。如果您想再次将 release/4.0.1
合并到 QA
,这会使事情变得复杂。阅读文档以获取有关 the consequences of reverting a merge.
- 不幸的是,无法过滤掉发生在 之间的其他分支的合并提交。在这个例子中,
N
发生在M1
和M2
之间,所以它会被剪掉。