将 merge 转换为 rebase,而无需再次执行 merge

Convert merge into rebase without having to perform the merge again

我犯了一个错误:我应该使用 git pull --rebase,但我发布了一个简单的 git pull,合并了所有内容,现在在我的分支的 HEAD 处有一个合并提交。

我想摆脱那个合并提交,我想我只是发出 git rebase -i HEAD~3,将我最后的本地提交移到顶部并将合并提交压缩到其中。 las,合并提交不可用于压缩。如果我这样做,我会进入中间状态,我需要再次进行合并,这是一项 很多 的工作。

有没有无需再次执行合并的解决方法?似乎应该可以以某种方式使用合并提交?

tl;博士

即使你可以在变基中包含合并提交,你也不能压缩合并提交。 Git 会通过以下方式通知您:

Refusing to squash a merge: <SHA-1>

为了保留合并期间所做的更改,您可以做的是将合并提交转换为正常提交。那时你可以像任何其他提交一样压缩它。

假设 HEAD 指向合并提交:

git reset --soft HEAD~1  # Keeps changes in the index
git commit               # Create a new commit, this time not a merge commit
git rebase -i HEAD~4     # Do an interactive rebase and squash the new commit

在变基期间保留合并提交

通常,您可以在使用 git rebase -p 进行变基时保留合并提交。
但是,它的目的是 重放导致合并 的提交。合并提交本身的任何更改(例如冲突解决)都不会保留。

来自 documentation:

-p
--preserve-merges
Recreate merge commits instead of flattening the history by replaying commits a merge commit introduces. Merge conflict resolutions or manual amendments to merge commits are not preserved.
This uses the --interactive machinery internally, but combining it with the --interactive option explicitly is generally not a good idea unless you know what you are doing (see BUGS below)

文档中提到的 bug 是由重新排序提交触发的。