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.
Key ideas
- 01
git logshows commit history, most recent first —git log --onelinecondenses each commit to one line (short SHA + message),git log -pshows the full diff of every commit, andgit log --graph --oneline --alldraws an ASCII graph of branching history, genuinely useful once multiple branches exist (Phase 2). - 02
git logaccepts 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, andgit log -- path/to/filerestricts history to commits touching that specific file or folder — these combine freely for genuinely targeted history searches. - 03
git show <sha>displays one specific commit in full detail — its message, author, date, and the exact diff it introduced. Given a SHA fromgit log, this is how you inspect any single commit's real content in isolation, without needing to check it out. - 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 withgit showon the SHA it reports to see the full context of that change. - 05
git blameoutput can be misleading on its own if a line was only reformatted (whitespace, renaming) rather than meaningfully changed —git blame -wignores whitespace changes, andgit log --follow -- filetracks a file's history even across renames, both genuinely useful refinements once blame's naive output stops making sense. - 06
A realistic real-world sequence:
git log --oneline -- src/PaymentService.javato find every commit touching that file,git show <sha>on the suspicious one to see its full diff and message, andgit blame src/PaymentService.javato see exactly which lines came from which commit — three tools, one investigation.
Code & diagrams
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.mdExplain it without notes
You need to find every commit that touched a specific file in the last month. Which git log flags do you combine, and why?
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
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.
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.