Rana Maninder Singh
Lesson 3 of 8Git & GitHub

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

ResourceLink
PreviousInstall & first repo
NextBranching & 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.md

Plain English: git log is your project's diary. --oneline compresses each entry to one line — use it daily.

Inspect one commit:

git show abc1234

Shows author, date, message, and exact line changes.


Section 2 — Diff — what changed?

CommandShows
git diffUnstaged changes (working vs staging)
git diff --stagedStaged changes (staging vs last commit)
git diff HEAD~1Compare to previous commit
git diff README.md

Reading output: lines with - were removed; lines with + were added.


Section 3 — Undo without panic

Discard unstaged edits (one file)

git restore README.md

Unstage a file (keep edits)

git restore --staged README.md

Revert a published commit (safe for shared repos)

Creates a new commit that undoes a bad one:

git revert abc1234

git reset --hard destroys uncommitted work and rewrites history. Never use on main after pushing unless your team explicitly allows force-push. Prefer git restore for 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 pop

Use 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 reset

Plain English: Binary search through history — finds the breaking commit in log₂(n) steps.


Practice

  1. Make 3 commits on a test file
  2. Run git log --oneline --graph
  3. Change a line, run git diff, then git restore to undo
  4. Stage a file, then git restore --staged to unstage

Checklist

  • [ ] I can read git log --oneline
  • [ ] I know git diff vs git diff --staged
  • [ ] I use git restore instead 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:

Common mistakes

  1. Undoing before reading status.
  2. Hard reset to "clean quickly" on shared work.
  3. Amending commits already pushed.

What's next

Branching & merging — parallel work without breaking main.