Comparing text and code is a fundamental development task: code review, branch merging, configuration audits, and contract checks. This article covers algorithms, tools, and practical comparison methods.
To quickly compare two texts or code snippets, use Diff Checker on rechecker.ru.
Why compare text
| Scenario | Description |
|---|---|
| Code review | Finding changes between commits |
| Branch merging | Resolving merge conflicts |
| Configuration audits | Comparing dev and prod settings |
| Documents | Comparing contract versions |
| Localization | Checking translations |
| Plugins/themes | Finding changes after an update |
Comparison algorithms
Longest Common Subsequence (LCS)
This classic algorithm finds the longest common subsequence. Its complexity is O(n*m), where n and m are the lengths of the strings.
Файл A: ABCDEF
Файл B: ACDEFG
LCS: ACDEFMyers diff
An optimized algorithm used by Git. It minimizes the number of changes in the output. Its complexity is O(ND), where N is the sum of the lengths and D is the size of the diff.
Patience diff
A variant for code: it first matches unique lines, then fills in the rest. It produces more readable diffs for refactoring.
Output formats
Unified diff (standard)
--- file1.txt
+++ file2.txt
@@ -1,4 +1,4 @@
-старая строка
+новая строка
без изменений
без измененийChange symbols
| Symbol | Meaning |
|---|---|
| Unchanged (context) |
- | Removed |
+ | Added |
! | Changed (in some formats) |
Context diff
*** file1.txt
--- file2.txt
***************
*** 1,4 ****
- старый текст
общий контекст
--- 1,4 ----
+ новый текст
общий контекстCommand-line tools
diff
diff file1.txt file2.txt
diff -u file1.txt file2.txt # unified
diff -r dir1/ dir2/ # рекурсивно
diff -w file1 file2 # игнорировать пробелыgit diff
git diff HEAD~1 # последний коммит
git diff branch1 branch2 # между ветками
git diff --word-diff # по словам
git diff --stat # статистикаcolordiff
colordiff file1 file2 # цветной выводOnline tools
Online diff services are useful for:
- Comparing without installing Git
- Working with configurations and logs
- Quick checks during code review
- Comparing output from different sources
Diff Checker rechecker.ru supports text and code comparison with syntax highlighting and character-by-character mode.
Comparing by word and character
By line (default)
Each line is a unit of comparison. Suitable for code and structured text.
By word
Было: "старый текст"
Стало: "новый текст"
Diff: "старый" -> "новый" (остальное без изменений)By character
Used for short strings, passwords, and hashes. Shows exact differences.
Practical scenarios
Comparing Nginx configurations
diff -u /etc/nginx/sites-available/default /etc/nginx/sites-available/backupComparing JSON
diff <(jq -S . file1.json) <(jq -S . file2.json)Sorting keys (jq -S) removes the effect of field order.
Ignoring whitespace
diff -w file1 file2 # все пробелы
diff -B file1 file2 # пустые строки
diff -b file1 file2 # пробелы в конце строкCI/CD integration
File comparison is part of build pipelines:
# Пример: проверка изменений конфигурации
- name: Compare configs
run: |
diff -u config.prod.yaml config.staging.yaml || true# Git diff в отчёте
- name: Generate diff report
run: git diff HEAD~1 --stat > diff-report.txtComparing three or more versions
Three-way comparison (3-way merge) is used to merge changes from multiple branches. Tools: diff3, meld, kdiff3, built into Git.
diff3 ours.txt base.txt theirs.txtComparing in an IDE
| IDE | Keyboard shortcuts | Function |
|---|---|---|
| VS Code | Ctrl+Shift+P → "Compare" | Compare two files |
| PhpStorm | Ctrl+D on a file | Compare with... |
| Sublime | Ctrl+Shift+P | File: Compare |
| Vim | :diffthis | Comparison mode |
Recommendations
- Use unified format — it is more readable and standard for patches
- Ignore nonessential differences — whitespace, case, where appropriate
- Limit the context — for large files, show only the changed sections
- Preserve the encoding — UTF-8 for correct display of Cyrillic text
- Normalize before comparison — sort JSON keys, remove trailing whitespace
For regular comparison of configurations, logs, and code snippets, Diff Checker on rechecker.ru provides a quick result without installing additional software.