如何从 git 中的多个分支合并中删除一个分支?

发布于 2025-01-01 07:15:30 字数 575 浏览 0 评论 0原文

我刚刚将三个不同的分支合并在一起作为功能集的一部分,并推送结果,以便存储库树当前如下所示:

--- A --- A1 --- A2 --- A3 --
    |                         \
    + --- B1 --- B2 --- B3 --- B4-
    |                              \
    + --- C1 --- C2 --- C3 ---------C4

但是,我刚刚被告知 B 分支尚未准备好黄金时间,所以我需要撤消这些提交。如何回滚我的更改,以便我的新头 C5 成为 C3A3 提交的合并?完成后,我希望存储库如下所示:

    + --- 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

油饼 2025-01-08 07:15:30

我来自的阵营更喜欢重做糟糕的事情只要它没有被推动,以留下不那么冗长的历史记录,我通常这样做:

# go back to commit C3
git checkout C3

# redo the merge with only A
git merge branch_A

# make sure you've gotten the desired alteration
# git diff --stat branch_C
# git diff branch

# switch your C branch over to your redone version
git checkout -B branch_C

# otherwise if you change your mind, just go back to C4
# git checkout branch_C

编辑: 评论 #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:

# go back to commit C3
git checkout C3

# redo the merge with only A
git merge branch_A

# make sure you've gotten the desired alteration
# git diff --stat branch_C
# git diff branch

# switch your C branch over to your redone version
git checkout -B branch_C

# otherwise if you change your mind, just go back to C4
# git checkout branch_C

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 merging B4. The --detach option from the initial checkout was also removed as it's superfluous if the argument C3 isn't a branch name.

﹎☆浅夏丿初晴 2025-01-08 07:15:30

还原 C4(并提交该还原)​​,然后将 A3 合并到您刚刚创建的 C5 中。

--- A --- A1 --- A2 --- A3 -----------------------------
    |                         \                          \
    + --- B1 --- B2 --- B3 --- B4-                        \
    |                              \                       \
    + --- C1 --- C2 --- C3 ---------C4--- C5(reverts C4) -- C6

最终得到包含 B3 + A3 的 B4,以及包含 C3 + A3 的 C6。

我想你也可以通过变基来完成这一切,但我更喜欢在做一些混乱的事情时留下我的痕迹,这样我就可以看到我的错误以及我做了什么来修复它。 Rebase 撤销了历史,这让我抽搐。

Revert C4 (and commit that reversion), then merge A3 into the C5 you just created.

--- A --- A1 --- A2 --- A3 -----------------------------
    |                         \                          \
    + --- B1 --- B2 --- B3 --- B4-                        \
    |                              \                       \
    + --- C1 --- C2 --- C3 ---------C4--- C5(reverts C4) -- C6

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.

旧人九事 2025-01-08 07:15:30
# checkout before merge
git checkout C3
# make merge we want
git merge A3
# rebase any work on top of the new merge
git rebase --onto HEAD C4 branch_C
# use fixed version as branch_C
git branch -f branch_C HEAD
# aha! now you on the fixed branch_C
git checkout branch_C

比对 B 做同样的事情。我想你没有分支 C 和 B。 B 在“坏”合并之后进一步。

# checkout before merge
git checkout C3
# make merge we want
git merge A3
# rebase any work on top of the new merge
git rebase --onto HEAD C4 branch_C
# use fixed version as branch_C
git branch -f branch_C HEAD
# aha! now you on the fixed branch_C
git checkout branch_C

Than do exactly the same with B. I suppose you didn't branch C & B further after 'bad' merge.

ゃ懵逼小萝莉 2025-01-08 07:15:30

假设您尚未与其他人共享此存储库,您可以尝试 git rebase --onto

git rebase --onto <to> <from> <what>

我假设您的分支被命名为 A(位于 A3)、B(位于 B4)和 C(位于 C4):

git rebase --onto B3 A3 C

其中 B3 和 A3 实际上是提交 SHA 本身。

不过,我强烈建议在执行此命令之前备份您的存储库,以确保安全。

Assuming you haven't shared this repository with anyone else yet, you can try git rebase --onto.

git rebase --onto <to> <from> <what>

I'll assume your branches are named A (at A3), B (at B4) and C (at C4):

git rebase --onto B3 A3 C

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文