Lesson 03 of 05Concept

Read a Commit's Changes

Goal: Use git show to see exactly what one commit changed, and why that is different from git log.

git log tells you that something happened: who committed what message, and when. git show tells you what happened: the full diff a single commit introduced.

It is the command you reach for when a line of code looks wrong and you want to know which commit put it there, and what else that commit touched.

One commit, header and diff

git show takes any way of naming a commit and prints two things: the commit's metadata, then the diff against its parent.

With no argument it shows HEAD, the commit you are currently on.

The anatomy of git show

$ git show HEAD

commit 7c1f9ab3e5d2c4089b16e0f7a2d3c5e81b9f4a60
Author: Dana Rivers <dana@example.com>
Date:   Tue Mar 4 11:02:18 2025 +0000

    Fix total for empty carts

diff --git a/src/price.js b/src/price.js
index 3b18e51..8f1c2a0 100644
--- a/src/price.js
+++ b/src/price.js
@@ -1,3 +1,3 @@
 export function total(items) {
-  return items.reduce((sum, item) => sum + item.price);
+  return items.reduce((sum, item) => sum + item.price, 0);
 }

The header is the same information git log would show for this commit. Below it, the diff is read exactly like any other: - lines left, + lines arrived, and the unprefixed lines are unchanged context.

Naming the commit you mean

Anything that resolves to a commit works as an argument:

  • git show — the commit HEAD points at
  • git show HEAD~2 — two commits back along the first parent
  • git show 7c1f9ab — by abbreviated id, as printed by git log --oneline
  • git show main — the tip of a branch
  • git show HEAD@{1} — where HEAD was before the last move

Which commit each name picks

git show and git show main both print "Fix total for empty carts". git show HEAD~1 is "Add checkout page", and git show HEAD~3 the initial commit.

Which command answers which question

git log

  • Many commits, no file contents
  • Which commit changed this?
  • --oneline for a compact list

git show

  • One commit, with its full diff
  • What did that commit change?
  • Works on a branch name, id or HEAD~n

git diff

  • The gap between two states
  • What is different right now?
  • Working tree, staging area, or two commits

Just one file from a commit

$ git show HEAD:src/price.js

export function total(items) {
  return items.reduce((sum, item) => sum + item.price, 0);
}

With <commit>:<path> Git prints the file's whole contents as of that commit, instead of a diff. It is the quickest way to read an old version without checking anything out.

Finished reading?

Marking it complete records your progress and XP for this lesson.