Tour of Git
Advanced Git / Lesson 5.8

git blame — Who Changed What?

git blame shows you who last modified each line of a file, and in which commit. It's an essential debugging and investigation tool.

When to Use git blame

  • Debugging — "Who wrote this line? What was the context?"
  • Understanding code — "When was this logic added?"
  • Code review — "Who should I ask about this section?"

Basic Usage

git blame filename.txt

Output looks like:

a1b2c3d (Alice  2024-01-15  1) function login(user) {
e4f5g6h (Bob    2024-02-20  2)   validateInput(user);
a1b2c3d (Alice  2024-01-15  3)   return authenticate(user);
i7j8k9l (Alice  2024-03-01  4)   logAttempt(user);
a1b2c3d (Alice  2024-01-15  5) }

Each line shows:

  • Commit SHA — which commit last changed this line
  • Author — who made the change
  • Date — when it was changed
  • Line number — the current line number
  • Content — the actual line of code

Reading the Output

Lines with the same SHA were all modified in the same commit. This tells you they were part of the same logical change.

In the example above:

  • Lines 1, 3, 5 are from Alice's original commit (a1b2c3d)
  • Line 2 was added by Bob later (e4f5g6h)
  • Line 4 was added by Alice in a newer commit (i7j8k9l)

Useful Flags

FlagPurpose
-L 10,20Blame only lines 10-20
-L :functionNameBlame a specific function
--since="2 weeks ago"Only show recent changes

git blame vs git log

ToolShows
git blame <file>Per-line attribution for current file state
git log -- <file>All commits that touched the file
git log -p -- <file>All commits with diffs for the file

Use blame to find who changed a line, then show or log to understand why.

Try it!

  1. The repo has a file with some history. View it: cat app.txt
  2. Run blame to see who changed each line: git blame app.txt
  3. Notice the different SHAs — each represents a different commit
  4. Check a specific commit's details: git show <sha> (use a SHA from the blame output)

Don't Use Blame to Blame

Despite the name, git blame is a learning tool, not a finger-pointing tool. Use it to understand the history and context of code changes. The goal is to find the right person to ask questions, not to assign fault.

Key Points

  • git blame <file> shows per-line commit attribution
  • Each line shows the SHA, author, date, and content
  • Use it to trace the history of specific lines
  • Combine with git show to understand the full context of a change
🎯

Goal

Use git blame to see who changed each line of app.txt

Terminal
$

No commits yet. Run `git init` and create your first commit.