[英]Git: Why does rebase result in conflicts while merge does not?
I'm probably not getting something right, but could anyone explain to me why git rebase
results in conflicts, while git merge
(same branch) does not?我可能没有得到正确的答案,但是谁能向我解释为什么
git rebase
导致冲突,而git merge
(同一分支)却不会?
For as far as I know git rebase
puts the commits from the other branch before the commits I made on my current branch, while git merge
takes those same commits and applies them to my branch as patch, right?据我所知,
git rebase
将来自另一个分支的提交放在我在当前分支上所做的提交之前,而git merge
采用相同的提交并将它们作为补丁应用到我的分支,对吗? Is the diff
then not the same, although maybe reversed?尽管可能相反,但
diff
是否不一样? Not sure why patching my branch with the other commits is not a problem, while patching the other branch with my commits is.不知道为什么用其他提交修补我的分支不是问题,而用我的提交修补另一个分支是。
I see that sometimes my question is still getting an upvote.我看到有时我的问题仍然得到赞成。 Let me explain for those that do not understand the currently chosen answer, because I sure didn't when I read it for the first time.
让我为那些不理解当前选择的答案的人解释一下,因为我第一次阅读时肯定没有。
Let's say you have branch master
with commits A
, B
and C
.假设您有提交
A
、 B
和C
分支master
。
Then from commit C
you create a new branch mybranch
.然后从提交
C
创建一个新分支mybranch
。 You commit, and you get commits D
and E
.你提交,你得到提交
D
和E
。
In the meantime someone else commits F
and G
on master.与此同时,其他人在 master 上提交了
F
和G
master
then looks like this: ABCFG
, while mybranch
looks like this: ABCDE
. master
看起来像这样: ABCFG
,而mybranch
看起来像这样: ABCDE
。
Now you have two merge strategies:现在您有两种合并策略:
While on mybranch
, you type git merge master
.在
mybranch
,您键入git merge master
。 It takes all the commits from master
that you do not have on mybranch
- F
and G
.它需要来自
master
所有提交,而您在mybranch
F
和G
。 It first merges F
on top of your E
(last commit of mybranch
), and then merges G
on top of F
.它首先将
F
合并到您的E
( mybranch
最后一次提交) mybranch
,然后将G
合并到F
之上。
End result: ABCDEFG
.最终结果:
ABCDEFG
。 Despite these letters being in the same order, the commits were not done (or may not have been done) in chronological order, because actually F
and G
were (or could have been) done before D
and E
.尽管这些字母的顺序相同,但提交并未按时间顺序完成(或可能未完成),因为实际上
F
和G
已经(或可能已经)在D
和E
之前完成。 You will see a merge commit in this case as well.在这种情况下,您也会看到合并提交。
While on mybranch
, you type git rebase master
.在
mybranch
,您键入git rebase master
。 It takes all the commits from master
that you do not have on mybranch
- F
and G
and places those on top of your current branch, brining it in the state that master
is currently in (because you branched from master
and now get all commits that were done on master
since you branched off).它从
master
那里获取您在mybranch
- F
和G
上没有的所有提交,并将它们放在当前分支的顶部,使其处于master
当前所处的状态(因为您从master
分支,现在获得所有提交自从您分支以来,就在master
上完成了)。 It then takes your first commit - D
- and merges it on top of G
(last commit that was done on master
).然后将您的第一次提交 -
D
- 合并到G
之上(最后一次提交是在master
完成的)。 Then it takes E
and puts it on top of D
.然后它取
E
并将其放在D
之上。
End result: ABCFGDE
.最终结果:
ABCFGDE
。 It looks as if no branch was ever split off master
, and if master is one continuous work, because you kind of said "I want my branch to look as if it was just split off master and then my work was put on top of it".看起来好像没有分支从
master
分离出来,如果 master 是一个连续的工作,因为你有点说“我希望我的分支看起来好像只是从 master 分离出来然后我的工作放在它上面”。 It is the equivalent of checking out master
(now ABCFG
), creating a new branch mybranch
and then adding your commits ( ABCFGDE
).它相当于检出
master
(现在是ABCFG
),创建一个新分支mybranch
然后添加你的提交( ABCFGDE
)。
Without going into details, let's just say that it could be that merging master
's F
on top of mybranch
's E
might not result in a conflict, but merging mybranch
's D
on top of master
's F
might.如果没有进入细节,让我们只想说,这可能是因为合并
master
的F
之上mybranch
的E
可能不会导致冲突,但合并mybranch
的D
之上master
的F
威力。 It just really depends on which code was changed an if it was a change that is compatible with the previous commit.它实际上取决于更改了哪些代码以及是否与之前的提交兼容。 Merge conflicts may arise in both cases.
在这两种情况下都可能出现合并冲突。
During a rebase, you apply all commits of some branch on top of another one.在变基期间,您将某个分支的所有提交应用到另一个分支之上。 It is possible that one of those commits has a conflict that you solved in a subsequent commit.
这些提交之一可能存在您在后续提交中解决的冲突。 Hence, the merge operation has no conflict whereas the rebase lead to intermediate conflicts.
因此,合并操作没有冲突,而 rebase 会导致中间冲突。
See also git rerere
which allows you to automatically solve conflicts that you already solved.另请参阅
git rerere
,它允许您自动解决已经解决的冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.