Contents

Engineering Craft › Version Control (Git)

Diff

Seeing exactly what changed between commits, branches or your working copy.

Also known as: git diff, diff, differences

git diff shows exactly what changed between two states of your code. It’s how you check your work before you commit and how reviewers read your change.

git diff                   # working directory vs staging area (unstaged changes)
git diff --staged          # staging area vs last commit (what you're about to commit)
git diff HEAD              # all uncommitted changes vs last commit
git diff main..feature     # between two branches or commits
git diff --stat            # just a summary of files and line counts
git diff -- src/app.py     # limit to one path

Reading the output

@@ -10,7 +10,7 @@ def login(user):
     if not user.active:
         return None
-    token = make_token(user)
+    token = make_token(user, ttl=3600)
     return token
  • Lines starting with - (red) were removed; + (green) were added.
  • Lines with neither are unchanged context.
  • @@ -10,7 +10,7 @@ says where in the file: starting at line 10, 7 lines, in the old and new versions.

A modified line shows up as one removed plus one added line.

Habit worth building

Run git diff --staged before every commit. It’s the cheapest way to catch a stray console.log, a leftover debug flag or a secret. A code review is the same thing done by someone else, so reading your own diff first is the best preparation (see self-review).

Two commits can also be compared: git diff <hash1> <hash2>.