本文介绍了Git会合并完整文件或更改它的一部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 文档说,Git合并整合文件更改。我不确定它是否意味着它将替换完整的已更改文件或仅更改它的一部分。 让我举例说明我的担忧。有人A和B在同一个项目上工作,但在不同的分支上工作。 项目---分支---主人 --- B的分支 b分支在Master分支的第10个版本中分支。 Master v1--2 --...-- 10--11-- --v1- -v2-- 如果我们都更改了文件 MyFile.java 在v11 / v2中,我想将它合并到主v12上,Git是否会将文件从B的v2中完全移走,或者它只会合并添加的行(假设Master v10没有冲突或更改方法)? 例如,MyFile v11看起来像 1: 2:void someMethod(){} 3: 4:void anotherMethod(){} 5: 和B的MyFile v2看起来像 1:):void someMethod(){} 3: 4:void myCoolMethod(){} 5: 6:void anotherMethod(){} 如果MyFile为空,但存在,当分支发生时,将只合并挤压方法 myCoolMethod 放入代码中,或者将MyFile的全部内容从分支B复制到MyFile中分支A?解决方案没有复制涉及的文件。那更微妙;大致来说,合并涉及比较文件(或者说, blobs )逐行。 合并示例在你的问题中概述,Git将应用 3-way合并 。基本上,这种合并策略使用的不仅仅是你想要合并的两个提交,还要从他们最近的共同祖先中提取线索。 共同祖先中包含的信息允许Git弄清楚在许多情况下如何合并事物,例如双向合并(根本不使用公共祖先)会导致合并冲突并询问您的很多简单化合并策略,用户进行干预。 无冲突3路合并示例 例如,在以下情况下, 如果你告诉Git合并提交 B 和 C ,通过查看记录在共同祖先( A )中的文件内容,3路合并策略会意识到你所做的只是添加 foo 和之间的code> baz bar 记录在 C 中的版本中的行。因此,合并只会导致 3路合并过程中的冲突示例 然而,在某些情况下,共同的祖先仍然没有提供足够的信息来让Git自行解决问题。这就是你的问题所描述的例子。 即使Git试图在您的示例中执行3-way合并,合并冲突依然会出现,因为blob持有共同祖先中 MyFile.java 的内容是完全空的;因此,它无法提供线索在后代添加或删除的线索( B 和 C )的共同祖先( A )。 Git自己无法计算出要保留的版本是包含 foo\\\bar 或包含 foo \\\baz\\\bar ( \\\ 代表一个换行符,在这里)。因此,Git会停下脚步(发出合并冲突)并寻求您的帮助。 The docs say that Git merge integrate file changes. I am not sure if it means it replaces the complete changed file or only changed portions of it. Let me illustrate my concern. There are guys A and B working on the same project but on different branches. Project--- Branch ---Master ---B's branchThe "b branch" is branched at 10th version of Master branch. Master v1--2--...--10--11-- --v1--v2--If we both changed the file MyFile.java in v11/v2 and I want to merge it on Master v12, will the Git completely move the file from B's v2 or it will only merge added lines (let's say that there are no conflicts or changed methods from Master v10)?For example, MyFile v11 looks like 1: 2: void someMethod() {}3:4: void anotherMethod() {}5:and B's MyFile v2 looks like 1: 2: void someMethod() {}3:4: void myCoolMethod() {}5:6: void anotherMethod() {}If the MyFile was empty, but existed, when the branching occurred, will merge only squeeze method myCoolMethod into the code or will copy the whole content of MyFile from branch B into the MyFile on branch A? 解决方案 There is no copying of files involved. It's more subtle that that; roughly speaking, a merge involves comparing files (or rather, blobs) line by line.In the simple merge example outlined in your question, Git would apply 3-way merging. Basically, this merging strategy uses, not just the two commits you want to merge, but also takes cues from their most recent common ancestor.The information contained in the common ancestor allows Git to figure out how to merge things on its own in many situations where a more simplistic merging strategy, such as a 2-way merging (which doesn't use the common ancestor at all), would cause a merge conflict and ask you, the user, to intervene.Example of a conflict-free 3-way mergeFor instance, in the following situation,if you told Git to merge commits B and C, the 3-way merging strategy, by looking at the contents of the file as recorded in the common ancestor (A), would realize that all you did was add the baz line in between the foo and bar lines in the version recorded in C. Therefore, the merge would simply result inExample of a conflict during a 3-way mergeHowever, in some cases, the common ancestor still doesn't provide enough information to allow Git to figure things out on its own. That is the case in the example described in your question.Even though Git would attempt to perform a 3-way merge in your example, a merge conflict would still arise, because the blob holding the contents of MyFile.java in the common ancestor is completely empty; therefore, it can't provide any cues as to where lines were added or deleted in descendants (B and C) of the common ancestor (A).Git, on its own, has no way of figuring out whether the version to keep is that which contains foo\nbar or that which contains foo\nbaz\nbar (\n stands for a newline character, here). Therefore, Git will stop in its tracks (issue a merge conflict) and ask for your help. 这篇关于Git会合并完整文件或更改它的一部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 01:51