发布于 2026-09-27 · CATAITO

文本 diff 使用指南

「这两段文字到底差在哪」是文案、代码、配置日常都要回答的问题。本文讲清 diff 的三种粒度、LCS 算法原理、git diff 符号读法,以及合同与代码比对的最佳实践。附免费在线文本对比工具。

三种粒度:行 / 词 / 字符

粒度最小单位适合
行级一整行找不同:代码、配置、清单、合同条款(git diff 默认)
词级单词文章润色对比:句子里改了哪个词
字符级字符单行内的细微修改、排版校对

粒度越细,结果越啰嗦。日常「找不同」选行级——这也是 本站文本对比工具采用的粒度:它把行内字符级微调显示为一行删 + 一行增,配合行号一眼定位。

LCS 算法原理

主流 diff 工具(GNU diff、git、各在线工具)底层都是 LCS(最长公共子序列):在两段文本的所有行里,找出「按原顺序出现、且总长度最长」的公共行序列。

  • 被 LCS 保留的行 = 两边一致的上下文(灰色)
  • 旧版有、LCS 没有的行 = 删除(红色 −)
  • 新版有、LCS 没有的行 = 新增(绿色 +)

复杂度 O(n×m),两段各 3000 行的文本在浏览器里也是毫秒级完成;再长就有必要先截取相关段落。

git diff 符号怎么读

@@ -12,4 +12,5 @@
   timeout = 30
-  max_retries = 3
+  max_retries = 5
+  cache_enabled = true
  • - 开头:只存在于旧版本(删除)
  • + 开头:只存在于新版本(新增)
  • 空格开头:两边一致的上下文
  • @@ -12,4 +12,5 @@:hunk 头——旧版从第 12 行起 4 行、新版从第 12 行起 5 行

在线 diff 工具的颜色约定与 git 一致:绿加红减。会用一处,处处都会。

合同与文档比对实践

  1. 统一格式再比:Word 转 PDF 再复制、或从邮件正文复制,都可能引入空格、换行差异,先把两份文本清洗成纯文本。
  2. 警惕条款位移:插入一条新条款会让后面所有编号错位,逐行 diff 时注意大段连续「删+增」——很可能是位移而不是重写。
  3. 统计交叉确认:比对完看新增/删除行数,与对方声称的改动幅度对一下,防止漏看整段被删。

敏感合同不适合上传到云转换工具——用 纯本地处理的对比工具,文本不出浏览器。

代码与配置比对实践

  • 提交前自查:把改动前后的配置贴进 diff,确认没有把调试开关、密钥带进提交。
  • 环境差异定位:测试环境与生产环境的配置文件做 diff,是排查「本地好好的、线上出问题」的第一步。
  • 注意行尾空格:肉眼不可见但 diff 可见,很多「明明一样」的行其实差在行尾。

常见问题

Q:diff 的行级、词级、字符级有什么区别?

A:行级以整行为单位(git diff 默认),适合找不同;词级看行内改词;字符级最细。粒度越细越啰嗦,日常用行级。

Q:LCS 算法是怎么找差异的?

A:找出两段文本中「按原顺序出现且最长」的公共行序列作为未变部分,其余按位置标为删除或新增。git 与 GNU diff 同源。

Q:git diff 里的 +、-、@@ 符号怎么读?

A:+ 新增、- 删除、空格上下文;@@ 是 hunk 头,标注差异区在两版中的行号范围。

Q:比对合同或法律文本要注意什么?

A:先统一格式再比;警惕条款编号整体位移;用统计交叉确认改动幅度。敏感文本用本地工具比对。

Q:为什么同一次修改有时显示一行删加一行增?

A:行级 diff 的最小单位是整行,行内一个字符不同就会记为删旧行 + 增新行。要细看可把相关行单独再比。

免费在线文本对比

LCS 行级比对 · 行号定位 · 新增/删除高亮 · 纯浏览器本地处理

打开文本对比工具 →

Three granularities: line / word / char

GranularityUnitBest for
LineA whole lineSpotting changes: code, config, lists, contract clauses (git diff default)
WordA wordProse editing: which word changed in a sentence
CharacterA characterSubtle edits within a line, proofreading

Finer granularity means noisier output. Line-level is the daily default — our Text Diff tool uses it: character-level edits show as one deletion plus one addition, with line numbers for quick location.

How the LCS algorithm works

Mainstream diff tools (GNU diff, git, online checkers) are all built on LCS (Longest Common Subsequence): find the longest sequence of lines that appears in both texts in order.

  • Lines kept by the LCS = shared context (gray)
  • In the old version but not the LCS = removed (red −)
  • In the new version but not the LCS = added (green +)

Complexity is O(n×m); two 3,000-line texts still finish in milliseconds in a browser. For longer inputs, extract the relevant sections first.

Reading git diff markers

@@ -12,4 +12,5 @@
   timeout = 30
-  max_retries = 3
+  max_retries = 5
+  cache_enabled = true
  • - prefix: only in the old version (removed)
  • + prefix: only in the new version (added)
  • Space prefix: shared context
  • @@ -12,4 +12,5 @@: hunk header — old version 4 lines from line 12, new version 5 lines from line 12

Online diff tools follow the same color convention as git: green adds, red removes. Learn it once, use it everywhere.

Comparing contracts & documents

  1. Normalize formats first: Word→PDF→copy or email bodies can introduce whitespace and line-break differences; clean both sides to plain text.
  2. Watch for clause shifts: inserting one clause renumbers everything after it — long runs of paired deletions/additions are usually shifts, not rewrites.
  3. Cross-check the stats: compare the add/remove counts against what the other side claims, so a deleted paragraph can't slip through.

Sensitive contracts shouldn't go through cloud upload tools — use a fully local diff tool; text never leaves the browser.

Comparing code & config

  • Pre-commit self-check: diff your config before/after to ensure debug flags and secrets don't slip into the commit.
  • Environment drift: diffing test vs production config files is step one of "works locally, breaks in prod".
  • Trailing spaces: invisible to the eye but visible to diff — many "identical" lines differ only at the end.

FAQ

Q: Line vs word vs character diff?

A: Line-level (git diff default) for spotting changes; word-level for edits within sentences; character-level for the finest detail. Finer = noisier; line is the daily choice.

Q: How does the LCS algorithm work?

A: It finds the longest in-order sequence of common lines as the unchanged part; the rest is marked as removals or additions by position. Same basis as git and GNU diff.

Q: How to read +, -, @@ in git diff?

A: + added, - removed, space context; @@ is a hunk header showing line ranges in both versions.

Q: Anything special about comparing contracts?

A: Normalize formats first; watch for clause renumbering shifts; cross-check with the add/remove stats. Use a local tool for sensitive texts.

Q: Why does one edit sometimes show as a delete plus an add?

A: Line diff's unit is the whole line — one changed character makes it a removal + addition pair. Diff the two lines separately for detail.

Free Online Text Diff

LCS line comparison · Line numbers · Add/remove highlights · 100% browser-local

Open Text Diff Tool →