我目前在这个州有一个回购:
…a——b——c——d——e BranchA
\
…h——i——m——n——o BranchC
^
|
BranchB Head
并希望获得此状态:
…a——b——c——d——e BranchA
\
…h——i——————————m——n——o BranchC
^
|
BranchB Head
我知道如何修改分支以移动提交,但我只想" rebase" BranchC,不是BranchB 。
我该怎么做?
答案 0 :(得分:1)
仅使用rebase
无法实现您想要的效果。在初始状态,提交m
有两个父项:b
和i
。在期望的状态中有不同的父母:e
和i
。实际上,处于期望状态的m
与初始状态中的m
完全不同。
将e
合并到i
,然后在新提交的基础上重新定位n
和o
。放弃旧的m
,n
和o
。
您可以运行以下命令:
# Create a backup branch on commit `o`
# It is useful to restore the original status of the repo if something goes wrong
$ git branch backupC BranchC
# Create a working branch on commit `i` and check it out
$ git checkout -b work BranchB
# Merge `e` into `i`
$ git merge BranchA
# Rebase the last 2 commits of BranchC on top of the new branch
$ git rebase --onto work BranchC~2 BranchC
这就是全部。现在的分支是BranchC
。如果您对结果感到满意,现在可以删除分支backup
和work
:
$ git branch -D backup work
删除backup
分支后,旧提交m
,n
和o
将无法访问。
但如果出现问题(冲突)并且git merge
或git rebase
无法完成,您可以通过运行git rebase --abort
或git merge --abort
来中止流程并恢复初始状态(取决于在失败的命令上)然后:
# Move branch `BranchC` to its initial position (commit `o`)
$ git branch -f BranchC backup
# Restore the original HEAD
$ git checkout BranchB
# Remove the branches created during the process
$ git branch -D backup work