简体   繁体   English

Git:为什么 rebase 会导致冲突而合并不会?

[英]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 .假设您有提交ABC分支master

Then from commit C you create a new branch mybranch .然后从提交C创建一个新分支mybranch You commit, and you get commits D and E .你提交,你得到提交DE

In the meantime someone else commits F and G on master.与此同时,其他人在 master 上提交了FG

master then looks like this: ABCFG , while mybranch looks like this: ABCDE . master看起来像这样: ABCFG ,而mybranch看起来像这样: ABCDE

Now you have two merge strategies:现在您有两种合并策略:

Merge合并

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 FG It first merges F on top of your E (last commit of mybranch ), and then merges G on top of F .它首先将F合并到您的Emybranch最后一次提交) 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 .尽管这些字母的顺序相同,但提交并未按时间顺序完成(或可能未完成),因为实际上FG已经(或可能已经)在DE之前完成。 You will see a merge commit in this case as well.在这种情况下,您也会看到合并提交。

Rebase变基

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 - FG上没有的所有提交,并将它们放在当前分支的顶部,使其处于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 )。

Why rebase might result in a conflict while merge does not.为什么 rebase 可能会导致冲突,而合并不会。

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.如果没有进入细节,让我们只想说,这可能是因为合并masterF之上mybranchE可能不会导致冲突,但合并mybranchD之上masterF威力。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM