DevToolBoxZA DARMO
Blog

Diff Checker i Porownanie Tekstu: Kompletny Przewodnik z Przykladami Kodu

12 min czytaniaby DevToolBox

A diff checker is an essential tool that every developer, writer, and system administrator needs in their toolkit. Whether you need to compare two files, review code changes, or find differences between two texts, understanding how diff algorithms work will make you significantly more productive. This comprehensive guide covers everything from the fundamentals of text diff algorithms to practical code examples in Git, JavaScript, Python, and Bash. If you need a quick diff checker online solution, our free tool lets you compare text online instantly with syntax highlighting and side-by-side view.

Try our free online Text Diff Checker tool instantly.

What Is a Diff Checker?

A diff checker (also known as a text compare tool or file diff utility) is software that takes two pieces of text or two files as input and identifies the differences between them. The output, commonly called a "diff" or "patch," shows which lines were added, removed, or modified. The concept originated in the Unix world with the diff command created by Douglas McIlroy in 1974, and it has since become the foundation of every version control system, code review workflow, and document comparison tool in existence.

At its core, a diff checker solves the problem of efficiently answering the question: "What changed between version A and version B?" This seemingly simple question is actually a computationally interesting problem. The diff algorithm must find the minimal set of edits (insertions and deletions) that transform text A into text B. This is known as the edit distance or shortest edit script problem, and the most common solution is based on the Longest Common Subsequence (LCS) algorithm.

Modern online diff tools go far beyond simple line comparison. They offer syntax highlighting for code, character-level difference detection within changed lines, side-by-side and inline views, ignore whitespace options, and even semantic diff capabilities that understand the structure of programming languages. Whether you are performing a code diff during a pull request review or comparing configuration files after a deployment, understanding diff output is a critical developer skill.

How Diff Algorithms Work

The foundation of every diff checker is the Longest Common Subsequence (LCS) algorithm. Given two sequences, LCS finds the longest sequence of elements that appear in the same order in both inputs (but not necessarily contiguously). Once the LCS is computed, everything not in the LCS represents the differences: elements only in the first input are deletions, and elements only in the second input are additions.

The classic LCS algorithm uses dynamic programming with a time complexity of O(n*m) where n and m are the lengths of the two inputs. For large files, this can be slow. In 1986, Eugene Myers published a groundbreaking paper describing an algorithm that finds the shortest edit script in O(n*d) time, where d is the number of differences. Since most diffs have relatively few changes compared to the total file size, Myers' algorithm is dramatically faster in practice. This is the algorithm used by Git and most modern text diff tools.

The Myers algorithm works by exploring a graph of edit operations. It processes the two inputs and finds the shortest path from the top-left corner (beginning of both texts) to the bottom-right corner (end of both texts) in an edit graph, where horizontal moves represent deletions, vertical moves represent insertions, and diagonal moves represent matching characters or lines. The algorithm uses a clever greedy approach combined with linear space optimization to find this optimal path efficiently.

Line-by-line vs character-by-character diffing: Most tools first compare at the line level to find which lines changed, then optionally perform a character-level diff within those changed lines to highlight exactly which words or characters were modified. This two-pass approach is both efficient and produces the most readable output. Some specialized tools also offer word diff mode, which splits lines into words before comparing, producing output that is easier to read for prose and documentation.

Types of Diff Output

There are several standard formats for displaying text diff results. Each format has its strengths depending on the use case:

Unified Diff: The most common format, used by git diff and most modern tools. It shows changes with + (added) and - (removed) prefixes, with a few lines of context around each change. The header includes @@ hunk markers that indicate line numbers. This is the standard format for patches and code reviews.

Side-by-Side Diff: Displays the original and modified text in two columns next to each other, with changes highlighted. This format is very popular in graphical diff checker tools and web-based code review platforms because it makes it easy to visually scan for changes. Most diff checker online tools use this format.

Inline Diff: Shows the old and new versions interleaved in a single column, with deletions and additions color-coded (typically red for removed and green for added). GitHub uses this as one of its code review view options.

Context Diff: An older format that uses ! to mark changed lines and shows the before/after versions in separate sections prefixed with *** and ---. While less common today, some legacy systems still use this format.

Word Diff: Instead of comparing whole lines, this mode compares individual words. Git supports this with git diff --word-diff. This is particularly useful for comparing prose, documentation, or any content where changes occur within lines rather than across entire lines.

Common Use Cases for Diff Checking

Diff checking and text comparison are integral to many professional workflows:

Code Review: Every pull request and merge request is fundamentally a diff. Developers review code diff output to understand what changed, verify correctness, suggest improvements, and ensure coding standards are met. Tools like GitHub, GitLab, and Bitbucket all present diffs as the primary interface for code review.

Document Versioning: Writers, editors, and legal professionals frequently need to compare two files to track changes between document revisions. Whether it is a contract, a specification, or a blog post draft, text compare tools make it easy to see exactly what was added, removed, or reworded.

Configuration File Changes: System administrators compare configuration files (nginx.conf, docker-compose.yml, .env files) before and after changes to verify that only intended modifications were made. A single misplaced character in a config file can cause a production outage, so file diff verification is critical.

API Response Comparison: When testing APIs, developers often need to compare JSON or XML responses between environments (staging vs production) or between versions. A diff checker helps identify unexpected changes in API output.

Merge Conflict Resolution: When Git encounters conflicting changes from different branches, it presents a three-way diff showing the common ancestor, the current branch, and the incoming branch. Understanding diff output is essential for resolving these conflicts correctly without losing work.

Diff Checker Code Examples

Git Diff Commands

Git has the most powerful built-in diff checker available. Here are the essential git diff commands every developer should know:

# ===== Basic git diff commands =====

# Compare working directory with staging area (unstaged changes)
git diff

# Compare staging area with last commit (staged changes)
git diff --staged
# or equivalently:
git diff --cached

# Compare working directory with last commit (all changes)
git diff HEAD

# Compare two specific commits
git diff abc1234 def5678

# Compare current branch with another branch
git diff main..feature-branch

# Compare a specific file between commits
git diff HEAD~3 HEAD -- src/app.js

# ===== Advanced git diff options =====

# Word-level diff (great for prose and documentation)
git diff --word-diff
# Output: [-old word-]{+new word+}

# Word diff with color only (no markers)
git diff --word-diff=color

# Show only file names that changed
git diff --name-only HEAD~5

# Show file names with change status (Added/Modified/Deleted)
git diff --name-status main..feature

# Show diff statistics (insertions/deletions per file)
git diff --stat
# Output:
#  src/app.js    | 15 +++++++++------
#  src/utils.js  |  8 +++++---
#  2 files changed, 14 insertions(+), 9 deletions(-)

# One-line summary of changes
git diff --shortstat
# Output: 2 files changed, 14 insertions(+), 9 deletions(-)

# Ignore whitespace changes
git diff -w
# or: git diff --ignore-all-space

# Ignore blank line changes
git diff --ignore-blank-lines

# Show diff with 10 lines of context (default is 3)
git diff -U10

# Generate a patch file
git diff > my-changes.patch

# Apply a patch file
git apply my-changes.patch

# Check if a patch applies cleanly (dry run)
git apply --check my-changes.patch

JavaScript Text Diff (diff npm package)

The diff npm package is the most popular JavaScript library for computing text diff results programmatically. It provides multiple comparison functions for different granularity levels:

// npm install diff
const Diff = require('diff');

const oldText = `function greet(name) {
  console.log("Hello, " + name);
  return true;
}`;

const newText = `function greet(name, greeting) {
  console.log(greeting + ", " + name + "!");
  return true;
}`;

// ===== Line-by-line diff =====
const lineDiff = Diff.diffLines(oldText, newText);
lineDiff.forEach(part => {
  const prefix = part.added ? '+' : part.removed ? '-' : ' ';
  const lines = part.value.split('\n').filter(l => l);
  lines.forEach(line => console.log(prefix + ' ' + line));
});
// Output:
// - function greet(name) {
// + function greet(name, greeting) {
// -   console.log("Hello, " + name);
// +   console.log(greeting + ", " + name + "!");
//     return true;
//   }

// ===== Character-level diff =====
const charDiff = Diff.diffChars('hello world', 'hello there');
charDiff.forEach(part => {
  const color = part.added ? '\x1b[32m' : part.removed ? '\x1b[31m' : '';
  process.stdout.write(color + part.value + '\x1b[0m');
});
// Highlights exact character changes

// ===== Word-level diff =====
const wordDiff = Diff.diffWords(
  'The quick brown fox jumps over the lazy dog',
  'The slow brown fox leaps over the tired dog'
);
wordDiff.forEach(part => {
  if (part.added) console.log('[+] ' + part.value);
  else if (part.removed) console.log('[-] ' + part.value);
});

// ===== Generate unified patch =====
const patch = Diff.createPatch(
  'greeting.js',  // filename
  oldText,         // old content
  newText,         // new content
  'original',      // old header
  'modified'       // new header
);
console.log(patch);
// Output: standard unified diff format

// ===== Apply a patch =====
const applied = Diff.applyPatch(oldText, patch);
console.log(applied === newText); // true

// ===== Structured patch for multiple files =====
const structuredPatch = Diff.structuredPatch(
  'old/file.js', 'new/file.js',
  oldText, newText, '', ''
);
console.log(JSON.stringify(structuredPatch.hunks, null, 2));

Python Diff (difflib module)

Python includes the powerful difflib module in its standard library. It provides classes and functions for comparing sequences, including generating unified diffs, context diffs, and even visual HTML diff reports:

import difflib

old_text = """function greet(name) {
  console.log("Hello, " + name);
  return true;
}""".splitlines(keepends=True)

new_text = """function greet(name, greeting) {
  console.log(greeting + ", " + name + "!");
  return true;
}""".splitlines(keepends=True)

# ===== Unified diff (most common format) =====
diff = difflib.unified_diff(
    old_text, new_text,
    fromfile='greeting.js.orig',
    tofile='greeting.js',
    lineterm=''
)
print('\n'.join(diff))
# Output:
# --- greeting.js.orig
# +++ greeting.js
# @@ -1,4 +1,4 @@
# -function greet(name) {
# -  console.log("Hello, " + name);
# +function greet(name, greeting) {
# +  console.log(greeting + ", " + name + "!");
#    return true;
#  }

# ===== Context diff (older format) =====
ctx_diff = difflib.context_diff(
    old_text, new_text,
    fromfile='original', tofile='modified'
)
print('\n'.join(ctx_diff))

# ===== HTML visual diff report =====
d = difflib.HtmlDiff()
html = d.make_file(
    old_text, new_text,
    fromdesc='Original',
    todesc='Modified',
    context=True,  # show only changed sections
    numlines=3     # lines of context
)
with open('diff_report.html', 'w') as f:
    f.write(html)

# ===== SequenceMatcher for similarity ratio =====
seq = difflib.SequenceMatcher(None,
    ''.join(old_text), ''.join(new_text))
print(f"Similarity ratio: {seq.ratio():.2%}")
# Output: Similarity ratio: 72.41%

# ===== Get matching blocks =====
for block in seq.get_matching_blocks():
    print(f"  a[{block.a}:{block.a+block.size}] == "
          f"b[{block.b}:{block.b+block.size}] "
          f"(size={block.size})")

# ===== Get opcodes (edit operations) =====
for op, i1, i2, j1, j2 in seq.get_opcodes():
    print(f"  {op:8s} a[{i1}:{i2}] b[{j1}:{j2}]")
# Output:
#   equal    a[0:0] b[0:0]
#   replace  a[0:28] b[0:38]
#   equal    a[28:50] b[38:60]

# ===== Compare two files =====
with open('file1.txt') as f1, open('file2.txt') as f2:
    diff = difflib.unified_diff(
        f1.readlines(), f2.readlines(),
        fromfile='file1.txt', tofile='file2.txt'
    )
    print('\n'.join(diff))

Bash / Linux Diff Commands

Linux and macOS provide several command-line tools for text comparison. The classic diff command, along with enhanced alternatives, covers most use cases:

# ===== Basic diff command =====

# Compare two files (default output format)
diff file1.txt file2.txt

# Unified diff format (most readable)
diff -u file1.txt file2.txt
# Output:
# --- file1.txt  2024-01-15 10:30:00
# +++ file2.txt  2024-01-15 11:45:00
# @@ -1,4 +1,4 @@
# -old line 1
# +new line 1
#  unchanged line

# Side-by-side comparison
diff -y file1.txt file2.txt
# or with specific width:
diff -y -W 120 file1.txt file2.txt

# Show only lines that differ (with side-by-side)
diff -y --suppress-common-lines file1.txt file2.txt

# Ignore case differences
diff -i file1.txt file2.txt

# Ignore all whitespace
diff -w file1.txt file2.txt

# Ignore blank lines
diff -B file1.txt file2.txt

# Recursive directory comparison
diff -r dir1/ dir2/

# Brief output (just report if files differ)
diff -q file1.txt file2.txt
# Output: Files file1.txt and file2.txt differ

# ===== Enhanced diff tools =====

# colordiff: colorized diff output
# Install: apt install colordiff / brew install colordiff
colordiff -u file1.txt file2.txt

# vimdiff: side-by-side in Vim editor
vimdiff file1.txt file2.txt

# sdiff: interactive side-by-side merge
sdiff file1.txt file2.txt

# ===== Practical examples =====

# Compare command output
diff <(ls dir1/) <(ls dir2/)

# Compare sorted files
diff <(sort file1.txt) <(sort file2.txt)

# Compare remote file with local file
diff <(curl -s https://example.com/config.yml) local-config.yml

# Generate a patch file
diff -u original.txt modified.txt > changes.patch

# Apply a patch
patch original.txt < changes.patch

# Dry run (check if patch applies cleanly)
patch --dry-run original.txt < changes.patch

# Reverse a patch
patch -R original.txt < changes.patch

# Compare two strings directly
diff <(echo "hello world") <(echo "hello there")

How to Read Diff Output

Understanding diff output is a fundamental developer skill. Here is a breakdown of the unified diff format, which is the most common format you will encounter in Git, code reviews, and patch files:

File headers: The diff starts with --- a/file.txt (original file) and +++ b/file.txt (modified file). In Git, a/ and b/ are virtual prefixes representing the before and after versions.

Hunk headers: Lines starting with @@ indicate the location of changes. The format @@ -start,count +start,count @@ tells you the line range in the original file (prefixed with -) and the modified file (prefixed with +). For example, @@ -10,7 +10,8 @@ means the hunk starts at line 10 in both files, with 7 lines shown from the original and 8 lines from the modified version.

Changed lines: Lines starting with - (typically shown in red) were removed from the original. Lines starting with + (typically shown in green) were added in the new version. Lines with no prefix are unchanged context lines that help you locate the change in the file.

# Example: reading a unified diff

diff --git a/src/config.js b/src/config.js
index 8a3b5c1..f29d4e2 100644
--- a/src/config.js                    ← original file
+++ b/src/config.js                    ← modified file
@@ -12,8 +12,9 @@ const defaults = {      ← hunk header: line 12, 8→9 lines
   timeout: 3000,                       ← context (unchanged)
   retries: 3,                          ← context (unchanged)
-  baseUrl: 'http://localhost:3000',    ← REMOVED (red)
-  debug: false,                        ← REMOVED (red)
+  baseUrl: 'https://api.example.com', ← ADDED (green)
+  debug: true,                         ← ADDED (green)
+  verbose: true,                       ← ADDED (green, new line)
   headers: {                           ← context (unchanged)
     'Content-Type': 'application/json' ← context (unchanged)
   }                                    ← context (unchanged)

Patch files: A diff can be saved as a .patch file and applied to another copy of the original file using git apply patch-file.patch or patch -p1 < patch-file.patch. This is how open-source contributors share changes without direct repository access.

Diff statistics: Use git diff --stat to see a summary of changes showing the number of insertions and deletions per file, plus a visual bar graph. Use git diff --shortstat for a one-line summary like 3 files changed, 25 insertions(+), 10 deletions(-).

# git diff --stat output example:
 src/config.js     | 5 +++--
 src/app.js        | 12 ++++++------
 tests/config.test | 28 ++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 8 deletions(-)

# The bar shows the ratio of additions (+) to deletions (-)
# Longer bars = more changes in that file

Best Diff Tools Comparison

Here is a comparison of the best diff checker tools available, from our free online diff tool to powerful desktop applications:

ToolTypePricePlatformBest For
DevToolBoxWebFreeAny browserQuick online comparisons
VS CodeEditorFreeWin/Mac/LinuxCode review + editing
Beyond CompareDesktop$35-60Win/Mac/Linux3-way merge, folder sync
MeldDesktopFreeLinux/Mac/WinVisual diff + VCS integration
WinMergeDesktopFreeWindowsFile + folder comparison
diff (CLI)CLIFreeUnix/Linux/MacScripting + CI/CD pipelines
git diffCLIFreeAnyVersion control diffs

DevToolBox Text Diff (Online): Our free diff checker online tool provides instant side-by-side comparison with syntax highlighting, character-level diff within changed lines, and support for large texts. No installation required, works in any browser, and your data stays private (processed entirely client-side). Perfect for quick comparisons without leaving your browser.

VS Code Built-in Diff: Visual Studio Code includes an excellent diff editor. Right-click any file and select "Compare With..." or use code --diff file1.txt file2.txt from the terminal. VS Code provides inline and side-by-side views, character-level highlighting, and integrates with Git for reviewing staged changes.

Beyond Compare: A commercial diff tool with support for files, directories, FTP sites, and cloud storage. It offers three-way merge, folder synchronization, and hex comparison. Available for Windows, macOS, and Linux. Popular with teams that need advanced merging capabilities.

Meld: A free, open-source visual diff and merge tool for Linux (also available on macOS and Windows). It supports two-way and three-way comparison of files and directories, with Git, Mercurial, Subversion, and Bazaar integration.

WinMerge: A free, open-source diff and merge tool for Windows. It supports file and folder comparison, syntax highlighting, and shell integration. Its simplicity and reliability make it popular among Windows developers.

diff command (Unix/Linux): The original command-line diff tool. Available on every Unix-like system. While it lacks a graphical interface, it is fast, scriptable, and perfect for use in CI/CD pipelines and automated scripts.

Frequently Asked Questions

How do I compare two text files online?

To compare two text files online, paste the contents of each file into a diff checker tool like our free Text Diff Checker. The tool will instantly highlight added lines (in green), removed lines (in red), and unchanged context lines. Most online diff tools support side-by-side and inline views, character-level highlighting, and options to ignore whitespace differences. For large files, you can also use command-line tools like diff or git diff locally.

What does + and - mean in diff output?

In unified diff format (used by git diff and most modern tools), lines starting with + (plus) indicate content that was added in the new version, and lines starting with - (minus) indicate content that was removed from the original version. Lines without a prefix are unchanged context lines. In color-coded displays, + lines are typically shown in green and - lines in red. The @@ markers at the beginning of each section (called a hunk) show the line numbers where the changes occur.

How does git diff work?

Git diff uses the Myers diff algorithm to find the minimal set of changes between two versions of a file. When you run git diff with no arguments, it compares the working directory with the staging area (index). git diff --staged compares the staging area with the last commit (HEAD). git diff HEAD compares the working directory with HEAD. You can also compare specific commits with git diff commit1 commit2, or specific files with git diff -- path/to/file. Git diff outputs in unified diff format showing file headers, hunk headers with line numbers, and changed lines prefixed with + or -.

Understanding how to use a diff checker effectively is a fundamental skill for developers, writers, and system administrators. From reading Git diff output during code reviews to comparing configuration files before deployment, the ability to quickly find differences between two texts saves hours of manual comparison. Whether you prefer command-line tools like diff and git diff, or graphical tools with side-by-side views, mastering diff comparison will make you significantly more productive. Bookmark this guide for quick reference, and try our free online tool for instant text comparison.

Compare two texts instantly with our free online Diff Checker tool.

𝕏 Twitterin LinkedIn
Czy to było pomocne?

Bądź na bieżąco

Otrzymuj cotygodniowe porady i nowe narzędzia.

Bez spamu. Zrezygnuj kiedy chcesz.

Try These Related Tools

±Text Diff Checker123Word Counter↕️Line Sort & Dedup

Related Articles

Git Commands Cheat Sheet: Niezbędne polecenia dla każdego programisty

Kompletna ściągawka poleceń Git: konfiguracja, branchowanie, merge, rebase, stash i zaawansowane workflow.

Git Rebase vs Merge: Kiedy używać którego (z wizualnymi przykładami)

Zrozum różnicę między git rebase a merge. Naucz się kiedy używać którego.

Git cherry-pick, revert i reset wyjaśnione

Naucz się kiedy używać git cherry-pick, revert i reset. Różnice i przypadki użycia.

Poradnik Diff Tekstu Online: Algorytmy, git diff i Najlepsze Praktyki

Szczegolowa analiza narzedzi i algorytmow diff tekstu. Myers, Patience, Histogram diff, format zunifikowany git diff, narzedzia terminalowe, jsdiff, Python difflib, diff semantyczny, merge trojdrozny, wykrywanie regresji CI/CD i najlepsze praktyki.