【问题标题】:Replacing a git merge with another?将 git merge 替换为另一个?
【发布时间】:2022-11-22 14:14:19
【问题描述】:

我有一个提交看起来像这样的分支:

A->B->C

AB 是来自其他尚未合并到master 的分支的合并。 C 包含对此功能分支的相关更改。

C 依赖于 ABA 刚刚发生了翻天覆地的变化。然而,这些变化确实不是影响部分代码C 取决于。我想用来自新A的合并替换来自A的合并.

在这种情况下,新的变化是基于A的旧脑袋之上的。但我想知道如何在新提交不是旧提交的孩子的情况下执行此操作。

换句话说,我通过以下方式构建了分支:

  • 签出master
  • 合并A
  • 合并B
  • 进行更改并将其提交为C

我现在可以通过再次执行这些操作(使用更新的 A)来创建我想要的分支,但我希望能够只替换 A 合并而不重建分支。

【问题讨论】:

    标签: git


    【解决方案1】:

    你可以玩一个诡计尽量避免重做你已经完成的所有合并(包括 A)但是你将重写历史,没有办法避免它。

    git checkout -b hack A # create a _hack_ branch where we will first get the _real_ contents of the new A, B and C
    git merge new-A # merge "the new A" into hack
    git merge B # merge B
    git merge C # merge C
    # this branch now has 3 commits that have _the contents_ of the _new_ A, B and C.... it's time to hack history to get this in
    git commit-tree -p A~ -p the-new-A -m "The comment for the new A" hack~2^{tree} # create a replacement commit for A
    # last command will print an ID, and we will use it in the next command
    git commit-tree -p commit-id-from-previous-command -p B^2 -m "Comment for B" hack~^{tree} # create a replacement commit for B
    # will print a commit id, will use it in next command
    git commit-tree -p commit-id-from-previous-command -p C^2 -m "Comment for B" hack^{tree} # create a replacement commit for C
    # again, we get a commit ID which should be the branch like what you wanted.
    

    做一个 git checkout,检查内容并检查它的历史(git log,gitk)....如果你喜欢它,那么

    git branch -f master the-commit-id # or no commit ID if you did a checkout of it
    git checkout master
    

    当然,您已经改写了历史,这意味着它会产生后果,因此请谨慎使用。

    【讨论】:

    • 希望有更简单的东西,但我在这里了解了commit-tree。谢谢 :)
    • 这是可行的,没有太多麻烦......只需要一点点外箱思考(当然知道技巧)。
    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2022-12-18
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多