git如何在文件中生成差异? [英] How does git generate diff in files?

查看:199
本文介绍了git如何在文件中生成差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

diff文件时git遵循什么机制? git如何告诉我两个文件之间的区别(两次提交等)? 它是否遵循任何以行号为键,值为哈希值的键值对机制,并且如果哈希值发生变化,则该行会被标记为已更改的行?

What mechanism does git follow while diffing files? How is git able to tell me the difference between the two files(two commits, etc)? Does it follow any key-value pair mechanism where line number is the key and value is the hash and if the hash changes, the line is marked as the changed line?

推荐答案

您最初询问的关于 binary 文件的问题,在Git中表示"Git决定的文件不是文本".对于此类文件,除非您提供特殊的差异驱动程序,否则Git不会尝试生成差异,它只会说这两个文件相同"或这两个文件不同". (差异驱动程序是一个外部程序:您可以指示Git运行该程序,并且该程序可以执行 it 对这对文件的任何操作,以生成可用的差异.)

Your original question asked about binary files, which in Git, means "files that Git has decided are not text". For such files, unless you provide a special diff driver, Git does not attempt to generate a diff, it only says "these two files are the same" or "these two files are different". (A diff driver is an external program: you can instruct Git to run this program instead, and this program can do whatever it wants to do with the pair of files, to generate a useable diff.)

您更新的问题(至少在此时为止)询问有关差异 text 文件的问题. Git内置了 LibXDiff 的修改版本. 此处的主要算法归功于Eugene Myers .另请参见 Myers diff算法与Hunt-McIlroy算法.有关diff算法的更加用户友好的介绍,请参见我停滞的书.实际上,您已经有了行散列的概念:这些diff算法比较符号,并且使用行哈希作为diff矩阵中的符号是它们查找逐行diff的方式.

Your updated question, at least as of this time, asks about diffing text files. Git has built into it a modified version of LibXDiff. The main algorithm here is due to Eugene Myers. See also Myers diff algorithm vs Hunt–McIlroy algorithm. For a somewhat more user-friendly introduction to diff algorithms, see the last section of chapter 3 of my stalled book. You are in fact onto something with the idea of line hashes: these diff algorithms compare symbols, and using a line-hash as the symbols in the diff matrix is how they find line-by-line diffs.

这篇关于git如何在文件中生成差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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