我有一个分支,其中包含多个提交:
A --- B --- C --- D
^
HEAD
我已经完成了该分支的工作并准备了拉取请求。但是我对当前的历史不满意:
我正在寻找一种将C分为A和B而又不会破坏D的干净方法。
答案 0 :(得分:1)
您需要重新设置基准两次:
git rebase -i HEAD^4
运行交互式变基pick
替换为e
(edit
的缩写)git reset HEAD~
git add -p
仅在文件中添加一些更改很有用git rebase --continue
现在,您已将提交分为两部分,历史记录应如下所示:
A --- B --- Ca --- Cb --- D
^
HEAD
您现在可以启动第二个基准,以将Ca
合并到A
中,并将Cb
合并到B
中:
git rebase -i HEAD^4
Ca
的行移到提交A
的行下方,并将pick
更改为s
(squash
的缩写) )。Cb
的行移动到提交B
的行下方,并将其设置为压入B
git rebase --continue
答案 1 :(得分:1)
我将在要使用的C上创建一个temp
分支,让我们假设main在D上(未指定,但是):
git branch -b temp C
# let's go back to A, keeping _all_ of C's content so you can decide what to keep:
git reset --soft A
# now get the files the way you would like to get it for A (you can compare with A and B, just so that you _really_ get what you want.
# when you are done
git commit -m "new A"
# temp is on the new A
# let's go back to C
git checkout C
# let's put it on top of temp
git reset --soft temp
# Get the files you want the to be on B (compare with HEAD~1 and B.. and C)
# when you are done
git commit -m "new B"
# let's put temp where we are
git branch -f temp
# finally, let's move whatever is left over from C that is not part of B
git checkout C
git reset --soft temp
git commit -m "New C"
git branch -f temp
# at this moment C and temp are exactly the same
git checkout temp
git cherry-pick D
# temp is like the branch you want
...就是这样。
如果实际上C是您希望B变成的样子,但对A进行了一些更改,则遵循有关“新A”的方法,然后
git checkout C
git reset --soft temp
git commit -m "New B"
git branch -f temp
git checkout temp
git cherry-pick D