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:
a1b2c3dora1b2. Git only needs enough to be unambiguous. HEAD— the commit you are on right now.HEAD~1(orHEAD^) — its parent.HEAD~2means two parents back,HEAD~3three, and so on.- A branch name like
main— the commit that branch points to. You can combine:main~2.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git log
git logwalks back fromHEAD, newest first: full hash, author and message of each commit.$ git log --oneline
--onelineis the compact version you will use most: short hash + message, one line each.$ git log --oneline --all
Wait, there is a commit we did not see! Plain
logonly follows parents from where you stand.--allalso shows commits reachable from other branches (here:experiment— branches come next lesson).$ git show HEAD
git showopens one commit: its details plus the diff against its parent — what this commit changed.$ git show HEAD~1
HEAD~1is the parent of HEAD: one step back along the line.$ git show HEAD~3
HEAD~3is three parents back — the very first commit. It has no parent, so every file shows up as new.$ git diff HEAD~2 HEAD
git diff A Bcompares any two snapshots. Here: everything that changed across the last two commits (waffles added, pancakes salted).$ git log --oneline HEAD~2
Most commands accept these names. This lists history as seen from two commits ago.
$ git show experiment
A branch name works too. You could also type the first 4+ characters of any hash from the log instead.
- What happened recently?
- What did this one commit change?
- How do these two versions differ?
- What have I not committed yet?
git log --onelinegit 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 madegit diff <A> <B>Compare any two snapshotsHEAD~2 / HEAD^ / a1b2Two parents back / parent / hash prefix3 Check your understanding
HEAD is on "Fix typos". Which commit is HEAD~2?
What does git log --oneline --all show that git log --oneline does not?
In a log you see a3f9c21 Fix login bug. Which command shows what that commit changed?
What does git diff HEAD~1 HEAD compare?
4 Practice for real
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.