The Hook (The "Byte-Sized" Intro)
git blame has an unfortunate name — it's not about blaming anyone. It's about context. For every line in a file, it shows the commit that last touched it, the author, and the date. When you find a confusing line, blame tells you who wrote it, when, and (via the commit message) why. It's not finger-pointing — it's code archaeology.
📖 What is git blame?
git blame annotates each line of a file with the commit SHA, author, and date of the last modification. It helps you understand the history and intent behind any line of code.
Conceptual Clarity
What blame shows:
abc1234 (Jane Doe 2026-01-15 10:30 +0530 42) const token = validateAuth();
def5678 (John Doe 2025-12-01 14:22 +0530 43) const user = getUser(token);
abc1234 (Jane Doe 2026-01-15 10:30 +0530 44) if (!user) throw new Error();
Useful flags:
| Flag | What It Does |
|---|---|
-L 10,20 | Blame only lines 10-20 |
-w | Ignore whitespace changes |
-M | Detect moved lines within a file |
-C | Detect code moved between files |
-e | Show email instead of author name |
--since | Only show blame since a date |
Real-Life Analogy
git blame is like margin notes in a shared document — each paragraph has an annotation showing who wrote it and when. You don't scan the edit history; you see the attribution right next to the text.
Visual Architecture
Why It Matters
- Context: Find out WHY a line exists by reading the commit message.
- Debugging: Identify the commit that introduced a problematic line.
- Code review: Understand the history before suggesting changes.
- Collaboration: Know who to ask about specific code.
Code
# ─── Blame entire file ───
git blame src/auth.js
# ─── Blame specific lines ───
git blame -L 40,60 src/auth.js
# ─── Ignore whitespace changes ───
git blame -w src/auth.js
# Shows the real author, not the person who reformatted
# ─── Detect moved code ───
git blame -M src/auth.js
# Attributes lines to original author even if moved within file
git blame -C src/auth.js
# Detects code copied/moved from OTHER files
# ─── See the commit that last changed a line ───
git blame -L 42,42 src/auth.js
# abc1234 (Jane Doe 2026-01-15) const token = validateAuth();
git show abc1234
# Now you see the full commit context
# ─── Blame at a specific commit ───
git blame abc1234~1 -- src/auth.js
# See blame state BEFORE that commitKey Takeaways
git blameshows the last commit that touched each line.- Use
-wto ignore formatting-only changes and find the real author. - Use
-Mand-Cto trace code that was moved or copied between files. - Follow up with
git show <sha>to see the full commit context.
Interview Prep
-
Q: What does
git blameshow? A: For each line in a file, it shows the commit SHA that last modified the line, the author, the date, and the line number. This helps trace the history and intent behind any line of code. -
Q: How do you ignore whitespace changes in
git blame? A: Usegit blame -w. Without this flag, a formatting commit (like changing indentation) would replace the original author attribution for every reformatted line. -
Q: How do you trace a line back further than
git blameshows? A: Rungit blame <sha>~1 -- <file>where<sha>is the commit shown in the current blame. This shows the blame state before that commit, revealing the previous author. Repeat to trace further back.