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

时间:2018-02-15 09:10:50

标签: 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

我知道如何修改分支以移动提交,但我只想" rebase" BranchC,不是BranchB

我该怎么做?

1 个答案:

答案 0 :(得分:1)

仅使用rebase无法实现您想要的效果。在初始状态,提交m有两个父项:bi。在期望的状态中有不同的父母:ei。实际上,处于期望状态的m与初始状态中的m完全不同。

e合并到i,然后在新提交的基础上重新定位no。放弃旧的mno

您可以运行以下命令:

# 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。如果您对结果感到满意,现在可以删除分支backupwork

$ git branch -D backup work

删除backup分支后,旧提交mno将无法访问。

但如果出现问题(冲突)并且git mergegit rebase无法完成,您可以通过运行git rebase --abortgit 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