Basic

Before I press enter

`git diff` paid off the first time I spotted a change I didn't remember making, a password field I'd meant to leave alone. This is the command that saved me from my own tired afternoons.

What you will see

login.py | 1 insertion(+)

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

The command that caught me committing changes I did not remember making. Let me edit a file, stage part of it, and see exactly what each view is showing me before I hit enter.

1

Set up two files and change only one. git diff reads two versions of the project and prints every line that separates them. Right now nothing is staged, so the whole change is uncommitted, git diff shows it.

echo "login" > login.py git add login.py git commit -m "Add login" echo "hardcoded" >> login.py

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[main abc1234] Add login 1 file changed, 1 insertion(+)
The commit above is the clean baseline. The appended line below it is the only unstaged change, so everything I see next is that one edit.
2

Run the command you reach for before staging anything. It prints only changes not yet staged, green-plus lines are additions, red-minus lines are removals. It is the last thing you look at before touching a file you are about to save.

git diff

Before you run it, say out loud what it will print. Then open the answer.

What it prints
diff --git a/login.py b/login.py +hardcoded
If the output is empty, there is nothing staged and nothing uncommitted, git is just telling you the working folder matches the last commit.
3

Stage the change, then ask what is in the box. git diff --staged compares the staging area against the last commit, so it shows exactly what you are about to seal in. That is the last glance before git commit.

git add login.py git diff --staged

Before you run it, say out loud what it will print. Then open the answer.

What it prints
diff --git a/login.py b/login.py +hardcoded
git diff (no flags) shows uncommitted edits. git diff --staged shows staged edits. Run the wrong one and you might miss what you are committing.
4

Compare two points in history instead of two working states. HEAD is the latest commit; HEAD~1 is the one before it. The diff between them shows what that last commit actually changed.

git diff HEAD~1 HEAD

Before you run it, say out loud what it will print. Then open the answer.

What it prints
diff --git a/login.py b/login.py +login +hardcoded
HEAD~2 is two commits back, HEAD~3 three, and so on. The ~ counts parents going backwards, so it stacks cleanly.
5

Squeeze the diff into a readable summary. --stat prints a short list of files and how many lines changed in each instead of the full patch. It is enough to see which files a commit touched without reading every line.

git diff --stat

Before you run it, say out loud what it will print. Then open the answer.

What it prints
login.py | 1 insertion(+)
It is a summary, not the content. Read it when you care which files moved, but read the plain diff when you need the actual lines.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Diff reads two states of the project and prints the lines that separate them.
1 Git reads two snapshots
↓
2 It compares them line by line
↓
3 Removed lines print with a minus
↓
4 Added lines print with a plus
↓
5 You decide what to keep
Why this works
Reading a diff

A diff is a line-by-line comparison:

- old line of text
+ new line of text

Lines starting with - were deleted, + were added, and the unmarked lines in between are unchanged context so you can see what the change is about. The + and - are not git being cruel, they are the fastest way to see the shape of a change at a glance.

When you have a choice

Which diff will you actually run

CommandShows
git diffworking folder vs staging
git diff --stagedstaging vs last commit
git diff HEAD~1last commit vs this one

Most of the time git diff is the right one. Reach for the others when you need a narrower or wider view.

Examples

What I haven't saved yet

git diff

This prints every change you have made that is not staged yet. Green-plus lines are additions; red-minus lines are removals. Run it right before you stage anything, and you stop accidentally committing stray edits.

What I'm about to save

git diff --staged

After you run git add, this shows exactly what is in your staging box compared to the last commit. Last glance before git commit. I run it almost every time, and it has caught more surprises than I care to admit.

Compare two points in history

git diff HEAD~2 HEAD

HEAD is your latest commit. HEAD~2 is two commits back. This shows everything that changed across that window. The ~ counts parents going back, so HEAD~1 is the previous commit and HEAD~3 is three back.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Working in a parallel universe
Previous Lesson
Working in a parallel universe
Next Lesson
Reading your own history
Reading your own history →