diff
A diff is a line-by-line record of the changes between two versions of a file or codebase, showing which lines were removed, which were added, and a few unchanged lines around them for context. The name is short for difference, and it comes from the diff command that has shipped with Unix-like systems since the early days of Unix.
Say a teammate fixes a bug in a pricing module where a function multiplied the price by the discount rate instead of by what’s left after the discount. Comparing the old file with the new one using diff -u prints a unified diff, the format most tools use today:
$ diff -u pricing_old.py pricing.py
--- pricing_old.py 2026-09-28 10:15:02.000000000 +0000
+++ pricing.py 2026-09-29 09:41:37.000000000 +0000
@@ -1,4 +1,4 @@
def apply_discount(price, rate):
"""Return the price after applying a discount rate."""
- discounted = price * rate
+ discounted = price * (1 - rate)
return round(discounted, 2)
The two header lines name the old file with --- and the new file with +++. The @@ -1,4 +1,4 @@ line opens a hunk, which is one region where the files differ. It reads as four lines starting at line 1 in the old file and four lines starting at line 1 in the new file.
Inside the hunk, a leading - marks a removed line, a leading + marks an added line, and a leading space marks a context line that both versions share. An edited line shows up as a removal followed by an addition, because the format only tracks whole lines.
GNU diff -u and git diff both include three context lines by default. That context lets a reader, and tools like the patch command, find the right spot even when the surrounding file has shifted a little.
Editing either version of the file below updates the unified diff live, including the @@ line ranges. Changing the number of context lines shows how hunks grow, shrink, and merge:
How It Shows Up in Practice
A Python developer reads diffs most often in code review. The changes view of a pull request on GitHub, GitLab, or Bitbucket is a diff between two branches. It swaps the bare - and + characters for red and green highlighting, and reviewers leave comments anchored to individual lines.
Diffs also show up at the command line. Git prints a unified diff for commands such as git diff and git show, adding two lines above the usual headers: a diff --git line and an index line with short hashes that identify the old and new file contents.
Python’s standard library can generate diffs too, through difflib. The unittest framework uses it to show how a failing multiline string differs from the expected value.
A diff saved to a file is usually called a patch, and applying it reproduces the change on another copy of the code. Open source projects traded emailed patches long before pull requests existed, and the Linux kernel still accepts changes this way.
Why Small Diffs Matter
Reviewers can only hold so much in their heads, so teams aim for small, focused diffs. A pull request that renames a variable across forty files and also fixes a bug buries the fix in noise. Splitting the rename and the fix into separate changes keeps each diff readable and makes a later rollback easier to reason about.
Formatting churn causes the same problem. A change that reflows whitespace or reorders imports alongside a real edit produces a diff where the meaningful lines are hard to spot.
Many Python teams run an autoformatter such as Black or Ruff in continuous integration so that layout rarely differs between versions. When it does, a flag like git diff -w, which ignores whitespace, filters the noise back out.
Related Resources
Tutorial
How to Use Git: A Beginner's Guide
Learn how to track your code with Git using clear, step-by-step instructions. Use this guide as a reference for managing projects with version control.
For additional information on related topics, take a look at the following resources:
- Introduction to Git and GitHub for Python Developers (Tutorial)
- Advanced Git Tips for Python Developers (Tutorial)
- How to Review AI-Generated Python Code Efficiently (Tutorial)
- How to Use GitHub Copilot Code Review in Pull Requests (Tutorial)
- Introduction to Git and GitHub for Python (Course)
- How to Use Git: A Beginner's Guide (Quiz)
- Introduction to Git and GitHub for Python Developers (Quiz)
- Advanced Git Tips for Python Developers (Quiz)
- How to Review AI-Generated Python Code Efficiently (Quiz)
- How to Use GitHub Copilot Code Review in Pull Requests (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Oct. 29, 2026