“git merge"如何详细工作? [英] How does 'git merge' work in details?

查看:24
本文介绍了“git merge"如何详细工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 'git merge' 背后的确切算法(或接近该算法).至少对这些子问题的回答会有所帮助:

I want to know an exact algorithm (or near that) behind 'git merge'. The answers at least to these sub-questions will be helpful:

  • git 如何检测特定非冲突更改的上下文?
  • git 如何发现这些确切的行中存在冲突?
  • git 会自动合并哪些内容?
  • 当合并分支没有共同的基础时,git 的表现如何?
  • 当合并分支有多个共同基础时,git 的表现如何?
  • 当我一次合并多个分支时会发生什么?
  • 合并策略之间有什么区别?

但是对整个算法的描述会好得多.

But the description of a whole algorithm will be much better.

推荐答案

您最好寻找 3 路合并算法的描述.一个高层次的描述应该是这样的:

You might be best off looking for a description of a 3-way merge algorithm. A high-level description would go something like this:

  1. 找到合适的合并基础 B - 作为两个新版本(XY)的祖先的文件版本),并且通常是最新的此类基础(尽管在某些情况下它必须返回更远的地方,这是 git 的默认 recursive 合并的功能之一)
  2. 执行 XBYB 的差异.
  3. 浏览两个差异中标识的更改块.如果双方在同一地点引入相同的变化,接受其中之一;如果一个人引入了变化而另一个人离开了该区域,则在决赛中引入变化;如果两者都在某个地方引入了更改,但它们不匹配,请标记要手动解决的冲突.
  1. Find a suitable merge base B - a version of the file that is an ancestor of both of the new versions (X and Y), and usually the most recent such base (although there are cases where it will have to go back further, which is one of the features of gits default recursive merge)
  2. Perform diffs of X with B and Y with B.
  3. Walk through the change blocks identified in the two diffs. If both sides introduce the same change in the same spot, accept either one; if one introduces a change and the other leaves that region alone, introduce the change in the final; if both introduce changes in a spot, but they don't match, mark a conflict to be resolved manually.

完整算法对此进行了更详细的处理,甚至还有一些文档(https://github.com/git/git/blob/master/Documentation/technical/trivial-merge.txt 作为其中之一,以及 git help XXX 页,其中 XXX 是 merge-basemerge-filemergemerge-one-file<之一/code> 可能还有其他一些).如果这还不够深入,总有源代码......

The full algorithm deals with this in a lot more detail, and even has some documentation (https://github.com/git/git/blob/master/Documentation/technical/trivial-merge.txt for one, along with the git help XXX pages, where XXX is one of merge-base, merge-file, merge, merge-one-file and possibly a few others). If that's not deep enough, there's always source code...

这篇关于“git merge"如何详细工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆