Git 移动合并提交(分支的第一次提交)

Git 移动合并提交(分支的第一次提交),git,Git,我目前在该州拥有回购协议: …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 并希望获得以下状态: …a——b——c——d——e BranchA \ …h——i——m——

我目前在该州拥有回购协议:

…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
并希望获得以下状态:

…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
我知道如何重新设置分支的基础以移动提交,但我只想“重新设置”branch,而不是BranchB


我将如何做到这一点?

仅使用
rebase
无法实现您想要的。在初始状态下,commit
m
有两个父级:
b
i
。在所需状态下,is具有不同的父级:
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
备份
分支被删除时,旧的提交
m
n
o
将无法访问

但是,如果出现问题(冲突)并且
git merge
git rebase
无法完成,您可以通过运行
git rebase--abort
git merge--abort
(取决于失败的命令)中止进程并恢复初始状态,然后:


e
合并到
i
中,然后在新提交的基础上重新设置
n
o
。丢弃旧的
m
n
o
@axiac write作为答案?此外,重定基址将使
n
o
无法访问,因此它们将被有效地“丢弃”。这很好。我只是错过了最初的合并。rebase允许我逐个提交解决冲突,这太完美了。谢谢