Finding Who Changed What with git blame

Reviewed & published by Brayan K

When you find a confusing line of code, the first question is "who wrote this, and why?" By the end of this lesson you'll use git blame to pin every line to a commit, and the pickaxe (git log -S) to find exactly when a piece of code appeared.

Part of the free Git course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

What You'll Learn

1️⃣ Annotating a File

git blame file tags every line with the commit, author, and date that last changed it. The ^ in front of a hash marks a boundary commit — the oldest commit in the range Git looked at, often the repo's first commit.

# git blame answers "who last changed THIS line, and in which commit?"
# It annotates every line of a file with the commit, author, and date.

git blame file.txt

# Each line reads:
#   <hash> (<author> <date> <line#>) <the line's content>
#
# The ^ in front of a hash marks a "boundary" commit — the oldest commit
# in the range shown (often the very first commit of the repo).

2️⃣ Narrowing the Search

Whole-file blame is noisy. Use -L start,end to focus on the lines you care about, pass a commit to see authorship at that point in time, and add -w so a whitespace-only reformat doesn't steal credit for the real author's code.

# Blaming a whole file is noisy. Narrow it to the lines you care about
# with -L (line range) — perfect when you're staring at one broken function.

git blame -L 2,3 file.txt        # only lines 2 through 3

# You can also blame the file as it looked at an OLDER commit:
git blame 12c3f595 -- file.txt   # authorship at that point in time

# Tip: -w ignores whitespace-only changes so a reformat commit doesn't
# steal the credit for code someone else actually wrote.
git blame -w file.txt

Your turn. Blame a file, then narrow to a specific line range. Fill in the two blanks.

# 🎯 YOUR TURN — replace each ___ then run it in a repo with a file.
# Goal: find who last touched a specific block of lines.

# 1) Blame the whole file first to see the layout
git ___ file.txt              # 👉 the command that annotates each line

# 2) Now narrow to just lines 5 through 8
git blame ___ 5,8 file.txt    # 👉 the flag that limits to a line range

# ✅ Expected: step 2 prints only lines 5–8, each tagged with the commit,
#    author, and date that last changed it.

3️⃣ The Pickaxe — When Did This Appear?

blame shows the last change to a line, but often you want to know when a piece of text first appeared. The pickaxe, git log -S "string", finds every commit where the count of that string changed — i.e. when it was added or removed. Add -p for the diff, or use -G regex to match any diff line touching a pattern.

Common Errors (and the fix)

Pro Tips

📋 Quick Reference

CommandPurpose
git blame fileAnnotate each line's last change
git blame -L 5,8 fileBlame only lines 5–8
git blame -w fileIgnore whitespace-only edits
git blame <commit> -- fileBlame the file at an older commit
git log -S "text" -pFind when text was added/removed
git log -G "regex"Match a regex anywhere in diffs

Mini-Challenge: Trace a Line's Story

No commands given this time — just the plan. Use blame and the pickaxe together to fully explain where one line of code came from.

# 🎯 MINI-CHALLENGE: trace a line's whole story.
# No commands given — just the plan. Run each step in a real repo.
#
# 1. Pick a line in a file and run git blame to see who last changed it
# 2. Copy a distinctive word from that line
# 3. Use the pickaxe "git log -S <word> --oneline" to find when it appeared
# 4. Add -p to see the exact commit that introduced it
# 5. Bonus: run "git blame <that-hash> -- file" to see the file back then
#
# ✅ Expected: you can name the author, the commit, and the date a given
#    line was introduced — not just who last edited it.

# your commands here

🎉 Lesson Complete!

Practice quiz

What does git blame show for a file?

  • The number of bugs per line
  • Who deleted the file
  • For each line, the commit, author, and date that last changed it
  • Only the most recent commit

Answer: For each line, the commit, author, and date that last changed it. git blame annotates every line with the last commit, author, and timestamp that touched it.

How do you blame only lines 10 to 20 of a file?

  • git blame -L 10,20 file
  • git blame -n 10,20 file
  • git blame --lines 10-20 file
  • git blame file 10 20

Answer: git blame -L 10,20 file. The -L flag limits blame to a line range: git blame -L 10,20 file.

What does the caret (^) prefix on a hash in blame output mean?

  • A merge commit
  • A reverted commit
  • An uncommitted change
  • The line dates to the very first (boundary) commit in the range

Answer: The line dates to the very first (boundary) commit in the range. A ^ marks a boundary commit — the oldest commit shown, often the repo's initial commit.

git blame tells you who last touched a line. What does it NOT directly tell you?

  • The author's name
  • The full history of every previous change to that line
  • The commit hash
  • The date

Answer: The full history of every previous change to that line. Blame shows the most recent change per line; to see earlier versions you follow the history with git log.

Which command finds every commit that added or removed a specific string?

  • git log -S "<string>" (the pickaxe)
  • git grep <string>
  • git blame -S
  • git find <string>

Answer: git log -S "<string>" (the pickaxe). git log -S (the pickaxe) finds commits where the count of a string changed — when it was added or removed.

What does git log -p add to the pickaxe search?

  • Performance mode
  • Parallel search
  • The patch/diff for each matching commit
  • Only merge commits

Answer: The patch/diff for each matching commit. -p shows the diff, so you see exactly how the matching string was added or removed.

git log -G differs from git log -S by...

  • -G is faster
  • -G matches a regex appearing anywhere in the diff, not just changed occurrence counts
  • -G searches commit messages
  • they are identical

Answer: -G matches a regex appearing anywhere in the diff, not just changed occurrence counts. -S matches when the number of occurrences changes; -G matches any diff line touching the regex.

Why does blame sometimes credit the wrong person after reformatting?

  • A bug in Git
  • Blame ignores merges
  • The author config was wrong
  • Whitespace/format-only edits become the 'last change'; use -w to ignore whitespace

Answer: Whitespace/format-only edits become the 'last change'; use -w to ignore whitespace. git blame -w ignores whitespace changes so reformat commits don't mask the real author.

How do you blame a file as it was at an earlier commit?

  • git blame --old file
  • git blame <commit> -- file
  • git blame -t file
  • git blame --history file

Answer: git blame <commit> -- file. git blame <commit> -- file shows authorship for the file's state at that commit.

On GitHub, the blame view is reached by...

  • The Issues tab
  • The Actions tab
  • Opening a file and clicking the 'Blame' button
  • It is not available

Answer: Opening a file and clicking the 'Blame' button. GitHub shows a per-line Blame view from any file page, mirroring git blame in the browser.

Continue this course

Frequently asked questions

Does git blame mean Git is accusing someone?

No — despite the name, blame is a neutral history tool. It answers 'which commit, author, and date last changed this line?' so you know who to ask about a piece of code or which change to read for context. Many teams think of it as 'annotate' rather than literally assigning fault.

Why does blame credit a refactor instead of the original author?

Blame shows the most recent commit to touch each line, so a reformatting or rename commit can mask the real author. Use git blame -w to ignore whitespace, and -C / -M to detect lines that were moved or copied, which often restores the original authorship.

What's the difference between git blame and git log -S?

git blame works per line of one file and shows the single last change to each line. git log -S (the pickaxe) works across history and finds every commit where a given string was added or removed — ideal for 'when did this code first appear?' Use blame for 'who', and -S for 'when'.

What does the ^ before a hash mean in blame output?

It marks a boundary commit — the oldest commit in the range Git is showing. Often that's the repository's initial commit. It simply means 'this line goes at least this far back; Git didn't look beyond the boundary'.

Can I see blame in GitHub instead of the terminal?

Yes. Open any file on GitHub and click the 'Blame' button. It shows the same per-line authorship as git blame, with links to each commit, and lets you step back through earlier versions of a line right in the browser.