Gitastic
Path › Part 1: The mental model › Lesson 3 of 16
03

Reading history

log, show, diff — and how to point at any commit.

1 Learn the idea

History is only useful if you can read it. This lesson is about looking: listing commits, peeking inside one, comparing two, and naming the commit you mean without copying long hashes around.

Ways to name a commit

  • A hash — full or just the first few characters: a1b2c3d or a1b2. Git only needs enough to be unambiguous.
  • HEAD — the commit you are on right now.
  • HEAD~1 (or HEAD^) — its parent. HEAD~2 means two parents back, HEAD~3 three, and so on.
  • A branch name like main — the commit that branch points to. You can combine: main~2.

2 Watch it happen

Explore a recipe book

Press Play or use → to step through. Watch the graph as each command runs.

7a15f5d Create recipe book7a15f5dCreate recipe …8d5542a Add pancakes8d5542aAdd pancakesc718347 Add wafflesc718347Add waffles9dee7b4 Try vegan pancakes9dee7b4Try vegan panc…77c57ef Salt the pancakes77c57efSalt the panca…Branch "experiment" — a movable pointerexperimentHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git log

    git log walks back from HEAD, newest first: full hash, author and message of each commit.

  2. $ git log --oneline

    --oneline is the compact version you will use most: short hash + message, one line each.

  3. $ git log --oneline --all

    Wait, there is a commit we did not see! Plain log only follows parents from where you stand. --all also shows commits reachable from other branches (here: experiment — branches come next lesson).

  4. $ git show HEAD

    git show opens one commit: its details plus the diff against its parent — what this commit changed.

  5. $ git show HEAD~1

    HEAD~1 is the parent of HEAD: one step back along the line.

  6. $ git show HEAD~3

    HEAD~3 is three parents back — the very first commit. It has no parent, so every file shows up as new.

  7. $ git diff HEAD~2 HEAD

    git diff A B compares any two snapshots. Here: everything that changed across the last two commits (waffles added, pancakes salted).

  8. $ git log --oneline HEAD~2

    Most commands accept these names. This lists history as seen from two commits ago.

  9. $ git show experiment

    A branch name works too. You could also type the first 4+ characters of any hash from the log instead.

Question
  • What happened recently?
  • What did this one commit change?
  • How do these two versions differ?
  • What have I not committed yet?
Command
  • git log --oneline
  • git show <commit>
  • git diff <A> <B>
  • git status / git diff
git log --oneline [--all]List commits (from every branch with --all)
git show <commit>One commit and the changes it made
git diff <A> <B>Compare any two snapshots
HEAD~2 / HEAD^ / a1b2Two parents back / parent / hash prefix

3 Check your understanding

Question 1

HEAD is on "Fix typos". Which commit is HEAD~2?

50770c7 Write intro50770c7Write intro265fdb7 Add chapter 1265fdb7Add chapter 14eb1601 Add chapter 24eb1601Add chapter 27886305 Fix typos7886305Fix typosHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Question 2

What does git log --oneline --all show that git log --oneline does not?

Question 3

In a log you see a3f9c21 Fix login bug. Which command shows what that commit changed?

Question 4

What does git diff HEAD~1 HEAD compare?

4 Practice for real

Your mission

Time-travel one file

Someone kept fiddling with the timeout in config.txt. Dig through history, find the original value from the commit that created the file, and bring config.txt back to that version as a new commit — without touching other files or rewriting history.

94849db Create config click to insert the hash into the terminal94849dbCreate configfaa0c3d Add readme click to insert the hash into the terminalfaa0c3dAdd readme053fe38 Raise timeout click to insert the hash into the terminal053fe38Raise timeout8681894 Add logo click to insert the hash into the terminal8681894Add logo1eed8f5 Tweak timeout click to insert the hash into the terminal1eed8f5Tweak timeoutHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (main) $