一次性将分支与子分支重新合并

问题描述 投票:0回答:4

我经常在分支上有子分支,我想将其重新设置到主线上。考虑一下:

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

我想将这三个

topicA
分支全部移到主线上。目前,我知道两种方法可以做到这一点:

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

    a.此时,我必须删除

    topicA_Branch1
    2
    ,并在现在重新调整基础的
    topicA_Branch3
    上的正确提交上手动重新创建分支。

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

    a.当开启

    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 rebase
4个回答
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} 表示 “rebase 之前 topicABranch1 的最后一个已知状态”


0
投票

恐怕这很麻烦,我不知道有任何现有的 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 。


0
投票

您可以将

git filter-branch
与适当的
--parent-filter
一起使用。像这样的东西:

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

这里

X
应该是不在主线上但在其他分支上的第一个提交的哈希值,而
Y
可以只是
HEAD
Mainline
Mainline
当前头的哈希值...


0
投票

是否有一个我想要的命令可以对分支进行变基并带来它的子分支?

是的,现在有!

十年后再次偶然发现这个问题,现在有了一个美妙的选择

--update-refs
,它完全可以满足您的要求:

... [main L|✔] >git log --oneline --graph --all
* 517711b (HEAD -> main) m4
* 1e39f07 m3
| * 9ea1b8a (topicA_Branch3) A6
| * 6f3fd41 A5
| * c2c1632 (topicA_Branch2) A4
| * bcbe7a9 A3
| * d807097 (topicA_Branch1) A2
| * 5570ac2 A1
|/  
* 709d93d m2
* e3dd1f6 m1
* 6df6959 .gitignore
... [main L|✔] >git rebase --update-refs main topicA_Branch3
Successfully rebased and updated refs/heads/topicA_Branch3.
Updated the following refs with --update-refs:
        refs/heads/topicA_Branch1
        refs/heads/topicA_Branch2
... [topicA_Branch3 L|✔] >git log --oneline --graph --all
* 6f13f5e (HEAD -> topicA_Branch3) A6
* ca3fccc A5
* 5909128 (topicA_Branch2) A4
* 88baa8c A3
* 204986f (topicA_Branch1) A2
* 2253d3b A1
* 517711b (main) m4
* 1e39f07 m3
* 709d93d m2
* e3dd1f6 m1
* 6df6959 .gitignore
... [topicA_Branch3 L|✔] >

这个选项是两年前在 git 版本 2.38.0 中添加的。

我一直使用它,并且我所有的变基别名在适用时都包含它:

$ git config --list | grep rebase
alias.ri=rebase -i --rebase-merges --update-refs
alias.rc=rebase --continue
alias.rur=rebase --rebase-merges --update-refs
$
© www.soinside.com 2019 - 2024. All rights reserved.