Skip to main content
C
CodeUtil

Text Diff Tools - Comparing Code and Documents Efficiently

Learn how diff algorithms work, integrate git diff into your workflow, resolve merge conflicts, and choose the right visual diff tools for comparing code and documents.

2025-04-0314 min
Related toolDiff Checker

Use the tool alongside this guide for hands-on practice.

The bug that made me love diff tools

I spent an entire afternoon debugging a production issue. The code looked identical to what was working yesterday. Turns out, someone had changed a single character deep in a config file. If I'd run a diff first, I would have found it in seconds.

Now I diff everything. Pull requests, config files, deployment artifacts. Understanding diff output is one of those fundamental skills that just keeps paying off.

Reading unified diff output

Unified diff is what git shows you. Once you can read it fluently, code reviews get much faster.

  • Lines starting with - were removed (red in most tools)
  • Lines starting with + were added (green)
  • Lines with no prefix are context - unchanged, just for reference
  • @@ -10,5 +10,7 @@ tells you line numbers and how many lines changed
  • I always look at the @@ header first to orient myself in the file

The git diff commands I use daily

Here are the git diff variations I actually use, not just the ones in the docs:

  • git diff: What have I changed but not staged yet?
  • git diff --staged: What am I about to commit?
  • git diff HEAD~3: What changed in the last 3 commits?
  • git diff main...feature: What does this feature branch add? (the three dots matter!)
  • git diff --stat: Quick summary of what files changed
  • git diff -w: Ignore whitespace - crucial when someone reformatted

Merge conflicts: they are not that scary

I used to dread merge conflicts. Now I see them as git asking me to make a decision it could not make automatically. That is actually reasonable.

  • <<<<<<< HEAD marks the start of your version
  • ======= is the divider
  • >>>>>>> feature-branch marks the end of their version
  • Your job: combine them intelligently, then delete all the markers
  • Always test after resolving - merged code can have subtle bugs
  • git diff after resolving shows what you actually changed

My git diff config

These config options made my daily diff experience much better:

  • git config --global diff.algorithm histogram: Better results for moved code
  • git config --global diff.colorMoved zebra: Highlights code that moved, not just added/deleted
  • I use delta as my pager - syntax highlighting makes a huge difference
  • --word-diff for documentation PRs where whole lines are paragraphs

Visual diff tools I recommend

Sometimes the terminal is not enough. Here is what I use:

  • VS Code built-in diff: Good enough for most things, right in my editor
  • Meld: Free, three-way comparison, great for complex conflicts
  • delta (terminal): When I want syntax highlighting without leaving the command line
  • The Diff Checker on this site: Quick comparisons when I am in a browser
  • Beyond Compare: When I need to diff entire directories

Preventing merge conflicts

The best conflict is the one that never happens. These habits reduce conflict frequency:

  • Merge main into your feature branch frequently - I do it daily
  • Keep feature branches short-lived
  • Communicate when multiple people touch the same files
  • Never mix formatting changes with logic changes in one commit
  • Consistent editor config across the team prevents whitespace wars

Diffing non-code files

Diff is not just for code. I use it for all kinds of comparisons:

  • JSON/YAML: Format identically first, then diff
  • SQL: I use the SQL Formatter on this site before diffing queries
  • Images: git can use image diff tools with the right config
  • Word docs: Convert to text first with textconv in .gitattributes
  • git diff --no-index works on any two files, even outside git repos

The bottom line

Learn to read diffs fluently. Configure git diff the way you like it. Use visual tools when they help. The time you invest in mastering diff pays back every single day.

Start with git diff basics, then gradually explore the options as you need them. And when you need a quick comparison, the Diff Checker on this site works without any setup.

FAQ

What is the difference between git diff and the regular diff command?

Regular diff compares any two files. Git diff is git-aware - it knows about commits, branches, staging area, and integrates with your repo history. Use git diff --no-index when you want git diff features on files outside a repo.

How do I resolve merge conflicts?

Find the conflict markers (<<<<<<, ======, >>>>>>), understand what both versions are trying to do, combine them intelligently, delete all markers, then git add and commit. Always test after resolving.

What diff algorithm should I use?

For code, use histogram: git config --global diff.algorithm histogram. It handles moved blocks better than the default. You will notice the difference in refactoring PRs.

How do I ignore whitespace in diffs?

git diff -w ignores all whitespace. git diff -b ignores changes in amount of whitespace. Essential when someone ran a formatter on a file.

How do I compare two branches?

git diff main..feature shows all differences. git diff main...feature (three dots) shows only what feature added since it branched off. The three dots version is usually what you want for PR reviews.

What is the best visual diff tool?

Depends on your needs. VS Code built-in is good for most things. Meld for complex three-way merges. delta for terminal with syntax highlighting. For quick web-based comparison, use the Diff Checker on this site.

Martin Šikula

Founder of CodeUtil. Web developer building tools I actually use. When I'm not coding, I experiment with productivity techniques (with mixed success).

Related articles