我经常在分支上设置子分支,我想将其重新绑定到主线上。考虑一下:
* (Mainline)
*
*
| * (topicA_Branch3)
| *
| *
| * (topicA_Branch2)
| *
| *
| * (topicA_Branch1)
| *
| *
|/
*
*
我想将所有这三个topicA
分支移动到主线上。目前,我知道有两种方法可以做到这一点:
在topicA_Branch3
上,执行命令git rebase Mainline
。
一个。此时,我必须删除topicA_Branch1
和2
并在现已重新定位topicA_Branch3
上的正确提交上手动重新创建分支。
另一种方法是执行三个单独的命令:
一个。在topicA_Branch1
上,执行git rebase Mainline
。
湾git rebase --onto topicABranch1 <topicA_Branch1-old-SHA> topicABranch2
℃。 git rebase --onto topicABranch2 <topicA_Branch2-old-SHA> topicABranch3
d。这有点麻烦......
是否有一个我想要的命令会重新分支一个分支并带上它的子分支?
要说清楚,我想最终得到这个:
* (topicA_Branch3)
*
*
* (topicA_Branch2)
*
*
* (topicA_Branch1)
*
*
* (Mainline)
*
*
*
*
答案 0 :(得分:0)
我担心这很麻烦,我不知道有任何现有的全部命令。但它是可编写脚本的。从第一个到第二个插图的命令是
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。
答案 1 :(得分: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
...
答案 2 :(得分:0)
那是怎么回事:
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"