合并GIT中合并分支的历史记录?

发布于 2024-08-05 16:25:53 字数 327 浏览 5 评论 0原文

在我的 git 存储库中,我已将分支“B”合并到“master”中,获得以下结构:

--> A --> B --> C --> D  (branch master)
    |           |  
    F --> G --> H        (branch B)

我现在想要合并分支以获得以下历史记录:

--> A --> B --> F --> G --> H --> D

我该怎么做?

问候, 约亨

In my git repository, I have merged branch "B" into "master", getting the the following structure:

--> A --> B --> C --> D  (branch master)
    |           |  
    F --> G --> H        (branch B)

I now want to combine the branches to get the following history:

--> A --> B --> F --> G --> H --> D

How can I do this?

Regards,
Jochen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一向肩并 2024-08-12 16:25:54

我假设 C 是合并提交,这就是您不希望将其包含在结果中的原因。

您需要做的就是 git rebase

让我们从另一个示意性提交图开始。留下原来的
分支完好无损,我们将在一个名为 rebasing 的新分支上进行操作,

git checkout -b rebasing master

--> A --> B------------------- --> C --> D         (branch master) (branch rebasing)
     \                           /
      +----+->  F --> G --> H -+                   (branch topic)

只需将 topicrebasing 之间的所有内容重新定位到 topic 上即可。

git rebase --onto topic topic rebasing

或者使用更短的命令执行相同的操作

git rebase topic

-> A -> B -----------------> C -> D                (branch master)
    \                      /
     +---+>  F -> G -> H +                         (branch topic)
                          \
                           +---------> B -> D      (branch rebasing)

现在,当我们查看rebase时,我们有一条从AD的直线。

-> A -> F -> G -> H -> B -> D                      (branch rebasing)

所以,现在唯一的问题可能是顺序与原来的不同
你期望的。您可以通过使用 git rebase 重新排序提交来轻松解决此问题
--interactive,如果你愿意的话。

或者您以稍微复杂的方式重新设置所有内容。让我们重新开始吧。

--> A --> B------------------- --> C --> D         (branch master) (branch rebasing)
     \                           /
      +----+->  F --> G --> H -+                   (branch topic)

首先将从 Cmaster(又名 D)尖端的所有内容放在
topic 的提示(又名 H):

git rebase --onto topic C master

-> A -> B ----------------> C -> D                (branch master)
    \                      /
     +----> F -> G -> H +                         (branch topic)
                          \
                           +---------> D           (branch rebasing)

最后一次 rebase,最后,我们就完成了。

git rebase B

     +----> F -> G -> H +                         (branch topic)
    /                     \
-> A -> B ----------------> C -> D                (branch master)
         \
          +------------------------> F -> G -> H -> D   (branch rebasing)

瞧!

-> A -> B -> F -> G -> H -> D                      (branch rebasing)

I assume C is a merge commit, and that's the reason you don't want to have it in your result.

What you need, to do what you want, is git rebase

Let's start of with yet another schematic commit graph. To leave the original
branches intact we will operate on a new branch called rebasing

git checkout -b rebasing master

--> A --> B------------------- --> C --> D         (branch master) (branch rebasing)
     \                           /
      +----+->  F --> G --> H -+                   (branch topic)

Just rebase everything between topic and rebasing onto topic.

git rebase --onto topic topic rebasing

or do the same with a shorter command

git rebase topic

-> A -> B -----------------> C -> D                (branch master)
    \                      /
     +---+>  F -> G -> H +                         (branch topic)
                          \
                           +---------> B -> D      (branch rebasing)

Now when we just look at rebasing we have a straight line form A to D.

-> A -> F -> G -> H -> B -> D                      (branch rebasing)

So, the only problem right now might be that the order is different from what
you expected. You could easily fix that by reordering commits with git rebase
--interactive
, if you want to.

Or you rebase everything in a slightly more complicated way. Let's start again.

--> A --> B------------------- --> C --> D         (branch master) (branch rebasing)
     \                           /
      +----+->  F --> G --> H -+                   (branch topic)

First take everything from C to the tip of master (aka. D) and put it on the
tip of topic (aka. H) :

git rebase --onto topic C master

-> A -> B ----------------> C -> D                (branch master)
    \                      /
     +----> F -> G -> H +                         (branch topic)
                          \
                           +---------> D           (branch rebasing)

One last rebase, at the end, and we are finished.

git rebase B

     +----> F -> G -> H +                         (branch topic)
    /                     \
-> A -> B ----------------> C -> D                (branch master)
         \
          +------------------------> F -> G -> H -> D   (branch rebasing)

Voila!

-> A -> B -> F -> G -> H -> D                      (branch rebasing)
生生漫 2024-08-12 16:25:54

我将从 A 分支,然后挑选您想要的提交

git checkout -b my-branch A
git cherry-pick B
git cherry-pick F
git cherry-pick G
git cherry-pick H
git cherry-pick D

I would branch from A and then cherry pick in the commits that you want

so:

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