简体   繁体   English

一次性重新分支带有子分支的分支

[英]Rebasing a branch with sub-branches all at once

I often have sub-branches on a branch that I want to rebase onto the mainline. 我经常在一个分支上设置子分支,我想将其重新绑定到主线上。 Consider this: 考虑一下:

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

I want to move all three of these topicA branches onto mainline. 我想将所有这三个topicA分支移动到主线上。 Currently, I know two ways to do this: 目前,我知道有两种方法可以做到这一点:

  1. While on topicA_Branch3 , execute the command, git rebase Mainline . topicA_Branch3 ,执行命令git rebase Mainline

    a. 一个。 At this point, I would have to delete topicA_Branch1 and 2 and manually re-create the branches on the correct commits on the now rebased topicA_Branch3 . 此时,我将不得不删除topicA_Branch12并在现在重新topicA_Branch3上的正确提交上手动重新创建分支。

  2. Another way would be to do three separate commands: 另一种方法是执行三个单独的命令:

    a. 一个。 While on topicA_Branch1 , do git rebase Mainline . topicA_Branch1 ,执行git rebase Mainline

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

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

    d. d。 This is kind of cumbersome... 这有点麻烦......

Is there a command that I want that will rebase a branch and bring it's sub-branches with it? 是否有一个我想要的命令会重新分支一个分支并带来它的子分支?

To be clear, I want to end up with this: 要清楚,我想最终得到这个:

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

I am afraid this is cumbersome, I am not aware of any existing do it all command. 我担心这很麻烦,我不知道有任何现有的全部命令。 But it is scriptable. 但它是可编写脚本的。 The commands to get from the first to second illustration are 从第一个插图到第二个插图的命令是

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

After you have verified things are correct you can delete the *._OrIg_ branches. 验证完毕后,您可以删除*._OrIg_分支。 With this you only have to keep the script up to date with the names and sequence of the branches. 有了这个,您只需要使用分支的名称和顺序使脚本保持最新。

Update: You can create a list of all the branches, in sequence, with 更新:您可以按顺序创建所有分支的列表

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

Here you are only dependent on knowing the name of the last topicA_BranchN branch, eg topicA_Branch3 in your example. 在这里,您只需要知道最后一个topicA_BranchN分支的名称,例如您的示例中的topicA_Branch3。

You could use git filter-branch with an appropriate --parent-filter . 你可以使用git filter-branch和一个合适的--parent-filter Something like this: 像这样的东西:

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

Here X should be the hash of the first commit that is not on mainline but is on the other branches, and Y could just be HEAD or Mainline or the hash of the current head of Mainline ... 这里X应该是第一的哈希承诺是不是主线,但在其他部门,并Y可能只是HEADMainline或现任掌门的哈希Mainline ...

What about that: 那个怎么样:

 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. ...

This might easily be automated. 这可能很容易实现自动化。 The syntax topicABranch1@{1} means "the last known state of topicABranch1 before the rebase" 语法topicABranch1 @ {1}表示“rebase之前的topicABranch1的最后已知状态”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM