Q14 of 40 · Git

How do you see who last changed a specific line in a file?

GitJuniorgitblamehistorydebuggingfundamentals

Short answer

Short answer: git blame <file> annotates every line with the commit SHA, author name, and date of the last change. git blame -L 42,55 <file> limits the output to lines 42–55. In VS Code or IntelliJ, 'Git Blame' / 'Annotate' overlays this inline. Use it to find who introduced a flaky test setup or an incorrect assertion.

Detail

git blame is the fastest way to answer "who changed this line and when?" — essential when debugging unexpected test behaviour or tracking down an assumption buried in a @BeforeEach method.

Useful flags:

  • -L 30,50 — limit to a line range
  • -w — ignore whitespace changes (don't blame someone for an indent-only commit)
  • -M — detect lines moved within the file (don't blame the person who moved them)
  • -C — detect lines copied from other files
  • --since=6.months — only show blames from the last 6 months

Following the real author: if a blame points to a "Reformat all files" commit or a merge commit, the real author is one step further back. git log <sha> -p <file> shows what that commit actually changed, and you can then blame the parent commit:

git blame -L 42,42 <file>   # find the SHA of the "format" commit
git log --follow <sha>^.. <file>   # find the actual change before it

// EXAMPLE

# Blame the whole file
git blame src/test/java/com/example/UserApiTest.java

# Output format:
# a1b2c3d4 (Alice Smith  2025-03-15 14:22:01 +0000 42)     given(reqSpec)

# Blame a specific line range (lines 40-55)
git blame -L 40,55 src/test/java/com/example/UserApiTest.java

# Ignore whitespace changes — don't blame the formatter
git blame -w src/test/java/com/example/UserApiTest.java

# Open blame in the terminal with commits coloured
git blame --color-lines -w src/test/java/UserApiTest.java

# Once you have the SHA, see what else that commit changed
git show a1b2c3d4

# Find ALL commits that touched a specific file over time
git log --follow --oneline -- src/test/java/UserApiTest.java

// WHAT INTERVIEWERS LOOK FOR

The -L flag for line ranges, -w for whitespace-immune blame, and the follow-up of git show <sha> to understand what the blamed commit actually did. Using blame to debug rather than just to assign fault is a maturity signal.

// COMMON PITFALL

Blaming a 'Reformat codebase' commit and assuming the reformatter wrote the logic. Always look at the content of the blamed commit with git show — if it's a style-only commit, blame the parent commit for the actual logic.