【问题标题】:Rebasing a branch with sub-branches all at once一次重新设置带有子分支的分支
【发布时间】:2014-05-11 07:39:38
【问题描述】:

我经常在分支上有子分支,我想重新定位到主线。考虑一下:

* (Mainline)
*
*
| * (topicA_Branch3)
| *
| *
| * (topicA_Branch2)
| *
| *
| * (topicA_Branch1)
| *
| *
|/
*
*

我想将所有这三个topicA 分支移到主线。目前,我知道两种方法:

  1. topicA_Branch3 上,执行命令git rebase Mainline

    一个。此时,我将不得不删除 topicA_Branch12 并在现在重新设置的 topicA_Branch3 上的正确提交上手动重新创建分支。

  2. 另一种方法是执行三个单独的命令:

    一个。在topicA_Branch1 上,做git rebase Mainline

    b. git rebase --onto topicABranch1 <topicA_Branch1-old-SHA> topicABranch2

    c。 git rebase --onto topicABranch2 <topicA_Branch2-old-SHA> topicABranch3

    d。这有点麻烦……

是否有我想要的命令来变基分支并带来它的子分支?

为了清楚起见,我想这样结束:

* (topicA_Branch3)
*
*
* (topicA_Branch2)
*
*
* (topicA_Branch1)
*
*
* (Mainline)
*
*
*
*

【问题讨论】:

  • 作为一个有趣的旁注,git filter-branch 包含自动执行此操作所需的所有机制。但是filter-branch 没有任何内置的rebase 功能。再加上它是一个非常大的(大)锤子,而不是这个相对简单的问题。 :-)

标签: git rebase


【解决方案1】:

那又怎样:

 a. While on `topicA_Branch1`, do `git rebase Mainline`.

 b. `git rebase --onto topicABranch1 topicABranch1@{1} topicABranch2`

 c. `git rebase --onto topicABranch2 topicABranch2@{1} topicABranch3` 

 d. ...

这可能很容易实现自动化。语法 topicABranch1@{1} 表示"the last known state of topicABranch1 before the rebase"

【讨论】:

    【解决方案2】:

    您可以将git filter-branch 与适当的--parent-filter 一起使用。像这样的:

    git filter-branch --parent-filter 'sed -e "s/X/Y/"' --tag-name-filter cat -- branchA branchB branchC
    

    这里X 应该是第一个不在主线但在其他分支上的提交的哈希值,Y 可能只是HEADMainline 或@ 的当前头部的哈希值987654328@...

    【讨论】:

      【解决方案3】:

      恐怕这很麻烦,我不知道有任何现有的 do it all 命令。但它是可编写脚本的。从第一个到第二个插图得到的命令是

      for i in topicA_Branch1 topicA_Branch2 topicA_Branch3
      do
          git branch $i._OrIg_ $i
      done
      # First branch directly
      git rebase --onto Mainline topicA_Branch1
      # Remaining branches
      git rebase --onto topicA_Branch1 topicA_Branch1._OrIg_ topicA_Branch2
      git rebase --onto topicA_Branch2 topicA_Branch2._OrIg_ topicA_Branch3
      

      确认一切正确后,您可以删除*._OrIg_ 分支。有了这个,您只需使用分支的名称和顺序使脚本保持最新。

      更新:您可以按顺序创建所有分支的列表,使用

       git rev-list --reverse --simplify-by-decoration Mainline..topicA_BranchN \
       | git name-rev --stdin --name-only
      

      在这里,您只需要知道最后一个 topicA_BranchN 分支的名称,例如topicA_Branch3 在您的示例中。

      【讨论】:

        猜你喜欢
        • 2022-11-09
        • 1970-01-01
        • 2011-08-01
        • 2012-02-15
        • 2010-12-06
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        相关资源
        最近更新 更多