Command Palette

Search for a command to run...

Hectal
PHASE 1Beginner ~14 min· topic 2 of 4

Topic 1.2

Viewing History: log, show, and blame

In one line

git log, git show, and git blame are the three tools for answering 'what happened here, when, and why' — genuinely essential for understanding any codebase you didn't write entirely yourself.

0/4 · 0%

Key ideas

  1. 01

    git log shows commit history, most recent first — git log --oneline condenses each commit to one line (short SHA + message), git log -p shows the full diff of every commit, and git log --graph --oneline --all draws an ASCII graph of branching history, genuinely useful once multiple branches exist (Phase 2).

  2. 02

    git log accepts powerful FILTERS: git log --author="name" shows only commits by a specific person, git log --since="2 weeks ago" filters by time, git log --grep="fix" searches commit MESSAGES for a term, and git log -- path/to/file restricts history to commits touching that specific file or folder — these combine freely for genuinely targeted history searches.

  3. 03

    git show <sha> displays one specific commit in full detail — its message, author, date, and the exact diff it introduced. Given a SHA from git log, this is how you inspect any single commit's real content in isolation, without needing to check it out.

  4. 04

    git blame <file> shows, LINE BY LINE, which commit last touched each line of a file and who authored it — genuinely essential for answering 'who wrote this and why' on an unfamiliar line of code, especially combined with git show on the SHA it reports to see the full context of that change.

  5. 05

    git blame output can be misleading on its own if a line was only reformatted (whitespace, renaming) rather than meaningfully changed — git blame -w ignores whitespace changes, and git log --follow -- file tracks a file's history even across renames, both genuinely useful refinements once blame's naive output stops making sense.

  6. 06

    A realistic real-world sequence: git log --oneline -- src/PaymentService.java to find every commit touching that file, git show <sha> on the suspicious one to see its full diff and message, and git blame src/PaymentService.java to see exactly which lines came from which commit — three tools, one investigation.

Code & diagrams

viewing-history.shmarkdown

Run these against any repo with a few commits of real history.

# The everyday view: compact, most recent first
git log --oneline

# Full detail, including diffs
git log -p -2
# (-2 limits it to the last 2 commits, since -p is verbose)

# A visual graph — genuinely useful once branches exist
git log --graph --oneline --all

# Filter by author, time, or message content
git log --author="Amit"
git log --since="1 week ago"
git log --grep="fix"

# History for just one file
git log --oneline -- README.md

# Inspect exactly one commit
git show <paste-a-real-sha-here>

# Line-by-line authorship for a file
git blame README.md

# Ignore pure whitespace changes when blaming
git blame -w README.md

Explain it without notes

01

You need to find every commit that touched a specific file in the last month. Which git log flags do you combine, and why?

02

git blame shows a line was last touched in a commit that just reformatted whitespace, not the commit that actually introduced the logic. What's the fix, and why does this happen?

Practice

01

Pick any file in a repo with real history and run git log --oneline -- <file>, then git show on the most recent SHA it lists to see that commit's full diff.

02

Run git blame on a file with a few commits of history and identify which commit (by its short SHA) introduced a specific line you pick at random.

Trade-offs

  • ↔

    Filtering and searching history (--grep, --author, path filters) is fast and powerful precisely because Git already has the ENTIRE history available locally with no network round-trip — but this also means the quality of what you can find is only as good as the commit messages people actually wrote; vague messages like "fix" or "updates" make even perfect search tools far less useful, which is exactly why Phase 7 covers writing commit messages that matter.

Done when you can

  • I can use git log with filters (--author, --since, --grep, path) to find specific commits.

  • I can inspect any single commit's full diff with git show.

  • I can use git blame to find who last touched a specific line and why.