git重写提交父级?

Itt*_*ayD 7 git

我有两个基于某个父级的提交:P -> A -> B

我希望他们基于另一个父母:P' -> A' -> B'

编辑:我的情况是我正在与其他团队成员一起工作。我们有一个中央 git 存储库。在我的配置中,'git pull' 确实会变基,而对于其他人,它会创建一个合并。时不时地,当我发出 'git pull' 时,git 开始重新提交我没有做的提交,我需要解决我没有编写的代码的冲突。这很长而且容易出错。

我认为原因是发生了类似的事情:

        /--A--B---- // my repository
       /
--C1--C2--------M-  // the remote
   \   M       /
    \-D-M'-E--/     // another developer
Run Code Online (Sandbox Code Playgroud)

因此,开发人员合并 C2,然后推送他的更改,这使 git 认为某些提交是新的(仅我的猜测)。我最终不得不改变其他人的提交。

Jaw*_*awa 15

让我们假设这种情况:

        A---B (topic1)
       /
...---P---C---D (topic2)
Run Code Online (Sandbox Code Playgroud)

如果您当前的分支是“topic1”,则可以通过发出git rebase topic2 topic1(或仅git rebase topic2)将提交 A 和 B 移动到 topic2 分支。然后回购图看起来像

                A'--B' (topic1)
               /
...---P---C---D (topic2)
Run Code Online (Sandbox Code Playgroud)

其中 commit D 是您认为的 P'。请注意,不需要更改新的父提交。

或者,更一般地说,如果您需要将某些提交作为任何提交的后代(不一定是分支的头)移动,您可以使用--onto参数:

         A---B (topic1)
        /
...Q---P---C---D (topic2)
Run Code Online (Sandbox Code Playgroud)

git rebase --onto topic2~3 topic1~2 topic1或者git rebase --onto Q P topic1Q 和 P 是提交的 SHA1。

     A'--B' (topic1)
    /
...Q---P---C---D (topic2)
Run Code Online (Sandbox Code Playgroud)

如果这些不是您想要的,请多解释一下您的情况?

  • +1 对`git-rebase` 的一个非常酷的解释——我写得不太好。 (2认同)