Lesson Completion
Back to course

git blame

Beginner
8 minutes4.8GitPlay to LearnLearn by Story

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:

FlagWhat It Does
-L 10,20Blame only lines 10-20
-wIgnore whitespace changes
-MDetect moved lines within a file
-CDetect code moved between files
-eShow email instead of author name
--sinceOnly 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

flowchart LR FILE["📄 Source File"] --> BLAME["git blame"] BLAME --> LINE1["Line 42: Jane, Jan 15<br/>abc1234"] BLAME --> LINE2["Line 43: John, Dec 1<br/>def5678"] BLAME --> LINE3["Line 44: Jane, Jan 15<br/>abc1234"] style BLAME fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 commit

Key Takeaways

  • git blame shows the last commit that touched each line.
  • Use -w to ignore formatting-only changes and find the real author.
  • Use -M and -C to 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 blame show? 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: Use git 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 blame shows? A: Run git 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.

Topics Covered

Git HistoryDebugging

Tags

#git#blame#debugging#history

Last Updated

2026-02-13