gpt4 book ai didi

git - 一种在一次提交中恢复整个分支的方法

转载 作者:太空狗 更新时间:2023-10-29 13:20:13 25 4
gpt4 key购买 nike

我需要像这样通过一次提交恢复整个分支:

          [topic]
o---o---o-------X
/
| [master]
---o---o---o

提交 X 必须具有类似 master~2 的状态(主题分支的起点)

我的解决方案是:

git rev-list --reverse master..topic | while read SHA; do
git revert -n ${SHA}
done
git commit -m "Reverted topic branch"

是否有更好(更短)的解决方案?

目的

想象一下,我有一种几乎基于 git-flow 的存储库。

              [release-3]
o---o---o---o
/ \ [fix-for-rc-3]
/ o---o---o
/
| [development]
---o---o---o

我有一个 development 分支,即将发布的 release-3 分支和一个所谓的 hot-fix-feature-for-rc-3 分支机构。在这个分支中,我正在做一些丑陋的 hack 来完成我的发布,但我根本不想在 development 中使用它,因为更多“正确”的解决方案已经落在这里,但无法应用由于某种原因到 release-3 分支。因此,我必须执行以下操作...

                                    [release-3]
o---o---o---o--------------------M
/ \ [fix-for-rc-3] /
/ o---o---o----------------X
/ \
| \[development]
---o---o---o---------------------------------D

我必须将 fix-for-rc-3 merge 到 release-3(点 M),然后执行“revert-all-this-shit”提交(X 点)并将其 merge 到 development(D 点)所以这段代码永远不会到达这里,即使整个 release-3 分支被 merge 到 development 然后发布完成。

这就是为什么我需要恢复整个分支...

问题

虽然根本问题已解决,但如果 topic 已 merge 到 release merge-base<,获取分支原因的分支点仍然存在问题 会失败。

最佳答案

您可以像这样创建一个恢复到分支基础的提交:

# on "topic" branch
git read-tree $(git merge-base topic master)
git commit -m "Reverted topic branch"
git checkout -- . # update working copy to match the committed tree

在您的示例使用中,您想要还原更改,以便您可以将分支 merge 回 master(或在您的示例中调用的 development),而无需实际 merge topic 分支中的任何更改。但这可以通过使用“我们的” merge 策略在不添加还原提交的情况下完成。来自git-merge documentation :

ours

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

例如:

git checkout master
git merge -s ours topic

关于git - 一种在一次提交中恢复整个分支的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19366142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com