History, Diff & Undo — Reading the Time Machine
Lesson opener
In one line: Git log, git show, git diff.
Why it matters: This shows up at work when you need to git log, git show, git diff.
Before this lesson: Install & first repo
Before you start
| Resource | Link |
|---|---|
| Previous | Install & first repo |
| Next | Branching & merging |
| Time | ~50 minutes |
What you will learn
git log,git show,git diff- Safely undo:
git restore,git restore --staged - When not to use
git reset --hard - Stash — park work-in-progress without committing
Section 1 — Reading history
git log
git log --oneline --graph --all
git log -3
git log -- README.mdPlain English: git log is your project's diary. --oneline compresses each entry to one line — use it daily.
Inspect one commit:
git show abc1234Shows author, date, message, and exact line changes.
Section 2 — Diff — what changed?
| Command | Shows |
|---|---|
git diff | Unstaged changes (working vs staging) |
git diff --staged | Staged changes (staging vs last commit) |
git diff HEAD~1 | Compare to previous commit |
git diff README.mdReading output: lines with - were removed; lines with + were added.
Section 3 — Undo without panic
Discard unstaged edits (one file)
git restore README.mdUnstage a file (keep edits)
git restore --staged README.mdRevert a published commit (safe for shared repos)
Creates a new commit that undoes a bad one:
git revert abc1234
git reset --harddestroys uncommitted work and rewrites history. Never use onmainafter pushing unless your team explicitly allows force-push. Prefergit restorefor learners.
Section 4 — Stash — save for later
You're mid-edit but need to switch tasks:
git stash push -m "WIP on sales chart"
git stash list
git stash popUse case: urgent bugfix on main while feature half-done.
Section 5 — Finding when a bug appeared
git bisect start
git bisect bad
git bisect good abc1234
# Git checks out middle commits — you test and mark good/bad
git bisect resetPlain English: Binary search through history — finds the breaking commit in log₂(n) steps.
Practice
- Make 3 commits on a test file
- Run
git log --oneline --graph - Change a line, run
git diff, thengit restoreto undo - Stage a file, then
git restore --stagedto unstage
Checklist
- [ ] I can read
git log --oneline - [ ] I know
git diffvsgit diff --staged - [ ] I use
git restoreinstead of deleting the whole folder
Teacher walkthrough: undo without panic
All figures illustrative.
Step 1 — Diagnose state. git status / git log --oneline before any undo.
Step 2 — Choose the tool. Unstaged edits → restore; staged → unstage; bad last commit (not pushed) → amend/reset per lesson rules; pushed → revert.
Step 3 — Prefer safe undos on shared branches (revert) over rewriting published history.
Step 4 — Verify. Status clean; log tells the story.
Failure example: reset --hard after push
Symptom: Teammate's commits "vanished"; collaboration broke.
Diagnosis: History rewrite on a shared branch.
Correction: Use git revert for published commits; coordinate before any force push (this course does not teach force-push as default).
Validation: Shared branch history remains understandable in git log.
Guided practice
You committed too soon locally (not pushed) and want to unstage one file but keep edits. What family of commands?
Complete solution: Unstage (restore --staged / reset path as taught) while leaving working-tree edits intact — not reset --hard.
Independent practice
Acceptance criteria:
- One safe undo of unstaged changes demonstrated.
- One explanation of why
reset --hardis dangerous. - When to prefer revert on shared main.
- Short note written before running any destructive undo.
Common mistakes
- Undoing before reading
status. - Hard reset to "clean quickly" on shared work.
- Amending commits already pushed.
What's next
Branching & merging — parallel work without breaking main.