将合并转换为变基,而无需再次执行合并 [英] Convert merge into rebase without having to perform the merge again
问题描述
我犯了一个错误:我应该使用 git pull --rebase
,但是我发布了一个简单的 git pull
,合并了所有内容,现在在我分公司的负责人.
I made a mistake: I should have used git pull --rebase
, but I issued a simple git pull
, merged everything and now have a merge commit at the HEAD of my branch.
我想摆脱那个合并提交,我想我只是发出一个 git rebase -i HEAD〜3
,将我的最后一个本地提交移到顶部,然后将合并提交压入它.,,合并提交不可用于压缩.如果这样做,我将进入需要再次进行合并的中间状态,这是一项工作.
I want to get rid of that merge commit and I thought I'd just issue a git rebase -i HEAD~3
, move my last local commit to the top and squash the merge commit into it. Alas, the merge commit is not available for squashing. If I do this, I get into intermediate states where I need to do the merge again, which is a lot of work.
是否有解决方法,而不必再次执行合并?似乎应该可以以某种方式使用合并提交?
Is there a way out of this without having to perform the merge again? It seems it should be possible to use the merge commit in some way?
推荐答案
tl; dr
即使您可以将合并提交包含在一个基准库中,也不能压制合并提交.Git会告诉您:
tl;dr
Even if you can include merge commits in a rebase, you can't squash merge commits. Git will let you know by saying:
拒绝压缩合并:< SHA-1>
Refusing to squash a merge: <SHA-1>
可以做的以保持在合并过程中所做的更改是将合并提交转换为普通提交.那时,您可以像其他任何提交一样压扁它.
What you can do in order to keep the changes made during the merge is to convert the merge commit into a normal commit. At that point you can squash it like any other commit.
假设 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
进行重新设置基准时,可以保留合并提交.
但是,其目的是重播导致合并的提交.合并中的任何更改都将自己提交(例如,冲突解决方案).
Preserving merge commits during a rebase
In general, you can preserve merge commits when doing a rebase using git rebase -p
.
However, its purpose is to replay the commits leading up to the merge. Any changes that are part of the merge commits themselves (e.g. conflict resolutions) aren't preserved.
来自文档:
-p
-保留合并
重新创建合并提交,而不是通过重播合并提交引入的提交来变平历史记录.合并冲突合并提交的决议或手动修订未保留.
这在内部使用-interactive
机制,但将其组合在一起显式使用-interactive
选项通常不是一个好主意除非您知道自己在做什么(请参见下面的错误)
-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)
错误文档重新引用提交会触发被引用.
The bug the documentation is referring to is triggered by reordering commits.
这篇关于将合并转换为变基,而无需再次执行合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!