如何从 git 中的多个分支合并中删除一个分支?
我刚刚将三个不同的分支合并在一起作为功能集的一部分,并推送结果,以便存储库树当前如下所示:
--- A --- A1 --- A2 --- A3 -- | \ + --- B1 --- B2 --- B3 --- B4- | \ + --- C1 --- C2 --- C3 ---------C4
但是,我刚刚被告知 B
分支尚未准备好黄金时间,所以我需要撤消这些提交。如何回滚我的更改,以便我的新头 C5
成为 C3
和 A3
提交的合并?完成后,我希望存储库如下所示:
+ --- B1 --- B2 --- B3 --- B4 --- B5 | / --- A --- A1 --- A2 --- A3 -- | \ + --- C1 --- C2 --- C3 --- C4 --- C5
I just merged three different branches together as part of a feature set and pushed the results so that the repository tree currently looks like the following:
--- A --- A1 --- A2 --- A3 -- | \ + --- B1 --- B2 --- B3 --- B4- | \ + --- C1 --- C2 --- C3 ---------C4
However, I have just been told that the B
branch as not ready for prime time so I need to undo those commits. How can I roll back my changes so that my new head C5
would be a merger of the C3
and A3
commits? When I am done I want the repository to look like the following:
+ --- B1 --- B2 --- B3 --- B4 --- B5 | / --- A --- A1 --- A2 --- A3 -- | \ + --- C1 --- C2 --- C3 --- C4 --- C5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我来自的阵营更喜欢重做糟糕的事情只要它没有被推动,以留下不那么冗长的历史记录,我通常这样做:
编辑: 评论 #4 是完全正确的,因为之前的版本没有任何结果。此编辑更改了
git merge
行,该行之前合并了B4
。初始签出中的--detach
选项也被删除,因为如果参数C3
不是分支名称,则该选项是多余的。I'm from the camp that prefers redoing botch ups as long as it hasn't been pushed, to leave a less verbose history and I usually do so like this:
Edit: Comment #4 was entirely correct in that the previous version of this resulted in nothing. This edit changes the
git merge
line, which was previously mergingB4
. The--detach
option from the initial checkout was also removed as it's superfluous if the argumentC3
isn't a branch name.还原 C4(并提交该还原),然后将 A3 合并到您刚刚创建的 C5 中。
最终得到包含 B3 + A3 的 B4,以及包含 C3 + A3 的 C6。
我想你也可以通过变基来完成这一切,但我更喜欢在做一些混乱的事情时留下我的痕迹,这样我就可以看到我的错误以及我做了什么来修复它。 Rebase 撤销了历史,这让我抽搐。
Revert C4 (and commit that reversion), then merge A3 into the C5 you just created.
You end up with B4 containing B3 + A3, and C6 containing C3 + A3.
I imagine you could do all this by rebasing too, but I prefer to leave my tracks when I've done something messy, so I can see both my mistake and what I did to fix it. Rebase undoes history, which makes me twitch.
比对 B 做同样的事情。我想你没有分支 C 和 B。 B 在“坏”合并之后进一步。
Than do exactly the same with B. I suppose you didn't branch C & B further after 'bad' merge.
假设您尚未与其他人共享此存储库,您可以尝试
git rebase --onto
。我假设您的分支被命名为 A(位于 A3)、B(位于 B4)和 C(位于 C4):
其中 B3 和 A3 实际上是提交 SHA 本身。
不过,我强烈建议在执行此命令之前备份您的存储库,以确保安全。
Assuming you haven't shared this repository with anyone else yet, you can try
git rebase --onto
.I'll assume your branches are named A (at A3), B (at B4) and C (at C4):
Where B3 and A3 are actually the commit SHAs themselves.
I strongly recommend backing up your repository before executing this command to play it on the safe side, though.