因为git是为源代码设计的,所以它的默认diff算法将一行视为最小的不可分单元。
我正在尝试编辑一些标记文件,这些文件在第80列是自动换行的。添加句子可能会导致段落的其余部分被标记为已更改。
有没有办法让Git使用更适合文本的diff算法?我需要一种将单词或句子视为不可分单元而不是行的方法?
发布于 2011-05-14 10:55:30
您可以尝试使用git diff --word-diff。
$ git diff --word-diff
diff --git a/test.txt b/test.txt
index 54585bb..a8cd97e 100644
--- a/test.txt
+++ b/test.txt
@@ -1,7 +1,7 @@
Because git is designed for source code, its diff algorithms {+are bibbity +}
{+bobbity boo+} treat a line as the minimum indivisible unit. I am trying to edit
some markdown files that are word wrapped at column 80. Adding a sentence can
cause the rest of the paragraph to be marked as changed.
Is there a way to have Git use a diff algorithm more suited to text? One that
treats words or sentences as indivisible units rather then lines?
No newline at end of file发布于 2011-05-14 10:55:38
也许你是在找word-diff
--单词差异=
<mode>
显示单词diff,使用<mode>来分隔更改的单词。默认情况下,单词由空格分隔;请参见下面的--word-diff-regex。<mode>默认为普通,并且必须是以下值之一:
颜色
仅使用颜色突出显示更改的单词。暗示着--颜色。素色
将单词显示为-删除-和{添加}。如果分隔符出现在输入中,则不尝试对其进行转义,因此输出可能不明确。
瓷器
使用一种专门用于脚本使用的基于行的特殊格式。添加/删除/未更改的运行以通常统一的diff格式打印,从行首的+/-/字符开始,一直延伸到行尾。输入中的换行符由波浪号~在其自身的一行中表示。无
再次禁用word diff。
请注意,无论第一个模式的名称是什么,如果启用,颜色将用于在所有模式中突出显示更改的部件。
http://git-scm.com/docs/git-diff
发布于 2011-12-13 13:39:31
这是一个自定义的例子(来自this question)。默认情况下,-- word -diff假设一个单词是一个非空格字符串。以下命令将认为单词由以下内容之一组成:
命令:
git diff --color-words --word-diff-regex='[A-z0-9_]+|[^[:space:]]'https://stackoverflow.com/questions/5999495
复制相似问题