假设我有这样一个存储库:
I --- C --- M master
\ /
`- A -´ topic
其中 M
是将 topic
merge 到 master
中的 merge 提交。
后来我在 C
中发现了一个错误,所以我在 master
分支上提交修复它,在 M
之上:
I --- C --- M --- C1 master
\ /
`- A -´ topic
但理想情况下,我希望历史看起来像这样:
I --- C --- C1 --- M master
\ /
`- A --------´ topic
如何重写历史,使 C1
出现在 merge M
之前?
我可以删除 M
,应用 C1
制作的补丁,然后再次将 topic
merge 到 master
中,再次解决所有冲突,但我想避免这种努力,如果可能的话,我更愿意保留原始提交信息(作者、日期等),这排除了执行 git commit
再次。我希望 git rebase
可以实现,但我失败了,-p
和 -i
中的一个或两个都失败了。
最佳答案
这可以通过单个 rebase 来移动提交(或任意数量的提交),然后从主题重做 merge 来完成。
# Create some branches just for readability
git branch mergeCommit master^
git branch beforeMerge master^^
# Run the rebase
git rebase --onto beforeMerge mergeCommit master
# After the rebase master will have the replayed commits on top of C
# Redo the merge
git merge topic
通过这个 rebase ,您可以在 merge 前移动多个提交。你可以阅读 plan english 中的 rebase 指令:
Grab the commits from
mergeCommit
tomaster
and rebase them on top ofbeforeMerge
.
关于git - 如何跨 merge 提交移动提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200768/