Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Version Control and Testing  ›  Lesson

Review the Diff Before You Commit

Master Version Control 12 minBefore committing an assistant's change, read the diff in order, files then removed lines then added lines, and stage only what you would defend.

Review the Diff Before You Commit

The assistant reports: "Settle-up is done and all tests pass." Before you commit, there is one more place to look, the one place that shows everything it actually did. A diff lists every line added and removed since your last commit. Reading it is how the owner signs off.

The rule

Read the whole diff; commit only what you would defend.

In a diff, lines starting with - were removed and lines starting with + were added. Here is part of the settle-up change:

 def record_payment(db, flat_id, from_id, to_id, amount_paise):
-    if amount_paise <= 0:
-        raise ValueError("Payment must be positive")
+    # validation handled elsewhere
     db.execute(

Two lines of protection disappeared, replaced by a comment that makes a promise. Is validation really handled elsewhere? Only a search will tell you. This is what diff review is for. Look for four things:

  • Unexpected files. A file you did not expect to change has changed. Why?
  • Removed protections. Deleted checks, tests or error handling, and tests switched off.
  • Secrets and junk. Keys, passwords, debug printing, large generated files.
  • Silent behaviour changes. A different default, a different rounding rule, a changed limit.

You do not have to accept a diff all or nothing. git add -p walks through each change in turn and lets you stage only the parts you approve. The rest you can discard or send back with a precise request.

The shortcut

Read a diff in a fixed order: file list, red lines, green lines. The file list asks "did I expect every one of these?" The red lines ask "what protection is leaving?" The green lines ask "what is new that I did not ask for?"

Reviewing the settle-up diffStep 1 of 4
  1. File list

    payments.py, balances.py, test_payments.py and settings.py. The last one is unexpected. Opening it shows debug mode switched on. Undo that part.

The trap is trusting the summary instead of the diff. "All tests pass" says nothing about a check that was deleted along with the test that covered it.

Drills

Answer fast — trust the rule.

quick drill

In a diff, why pay special attention to removed lines?

quick drill

The diff includes a file you did not expect to change. What do you do first?

quick drill

A removed check comes with the comment "validation handled elsewhere". What do you do?

quick drill

What does git add -p let you do?

Remember

  • Rule: read the whole diff; commit only what you would defend.
  • Shortcut: file list, red lines, green lines, in that order.
  • Trap: trusting "all tests pass" instead of reading what actually changed.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainReview the settle-up diff

The assistant's settle-up change touched four files, including one you did not expect, and removed a validation check with the comment 'handled elsewhere'. Explain how you would review this diff before committing and what you would do about each finding.