从不同特定分支中还原提交

3
假设我有主分支和功能分支A、B、C、D和E。
C、D和E合并到B中,B合并到主分支。 它们在开发过程中的不同时间都使用了--no-ff进行合并。
有一次客户决定要放弃D和E功能。 我可以查看提交列表并手动选择要还原的提交,但这需要太多的时间和精力,因为可能有数百个提交。 是否可能还原(在主分支或A中)仅来自D和E分支的所有提交?

你的问题会受益于ASCII艺术。 - Micha Wiedenmann
2个回答

3
您可以通过撤销合并提交来实现此操作。假设 D 在提交 abc123 被合并到 B 中; 在这种情况下,执行命令 git revert -m 1 abc123。对于将 E 合并到 B 的提交也是同样的方法。
请注意,如果您希望在未来重新引入 D 和/或 E 的更改,则不能再次将它们合并; 您需要撤销上述 git revert 命令创建的提交。换句话说,您需要“撤销撤销”。

1
如果你有两个合并的分支:
         h : merge commit
         |
         v
---------*-* B
        /
   ----* D

您可以使用以下命令列出来自 D 的提交记录:
git rev-list h^..D
#   h^ means : the first parent of h
# x..D means : any commit reachable from D and not reachable from x

你可以先通过视觉方式检查此列表是否只包含你想要撤销的提交:

gitk h^..D # or gitg, or git log --oneline --graph ...

撤销这个提交列表的众多方法之一可能是:

# example from branch A :

# create a working branch :
git checkout -b wip_removing_D
# revert the whole list of commits :
git revert --no-edit $(git rev-list h^..D)
# go back to the target branch :
git checkout A
# create a single commit which contains all of the reverts :
git merge --squash wip_removing_D
# delete working branch :
git branch -D wip_removing_D

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接