Git Blame: Track Who Changed Every Line of Code
Use git blame to see who last modified each line of a file, when the change was made, and in which commit. Learn advanced blame options for better code archaeology.
Detailed Explanation
Git Blame: Line-by-Line Code Archaeology
git blame annotates each line of a file with the commit, author, and date of the last modification. It is essential for understanding code ownership and change history.
Basic Usage
# Blame entire file
git blame src/auth/login.ts
Output:
abc1234 (Jane Doe 2025-03-15) import { hashPassword } from './crypto';
def5678 (Bob Smith 2025-04-02) import { validateEmail } from './validate';
ghi9012 (Jane Doe 2025-03-15)
abc1234 (Jane Doe 2025-03-15) export async function login(email, password) {
jkl3456 (Carol Lee 2025-05-10) if (!validateEmail(email)) {
Blame a Specific Line Range
# Lines 10-30 only
git blame -L 10,30 src/auth/login.ts
# From a function name
git blame -L '/function login/,/^}/' src/auth/login.ts
Ignore Whitespace Changes
# Skip commits that only changed whitespace
git blame -w src/auth/login.ts
Detect Code Movement
# Detect lines moved within the same file
git blame -M src/auth/login.ts
# Detect lines moved from other files
git blame -C src/auth/login.ts
# More aggressive detection (across the entire repo)
git blame -C -C -C src/auth/login.ts
Blame at a Specific Revision
# Blame the file as it was 3 commits ago
git blame HEAD~3 -- src/auth/login.ts
# Blame at a specific tag
git blame v2.0.0 -- src/auth/login.ts
Ignoring Bulk Reformatting Commits
When a commit reformats many files (e.g., running Prettier), it pollutes blame output. Use a .git-blame-ignore-revs file:
# .git-blame-ignore-revs
# Prettier reformatting
abc1234567890
# Configure git to use it
git config blame.ignoreRevsFile .git-blame-ignore-revs
Alternative: git log -p
For deeper history of a specific section:
# Full history of changes to specific lines
git log -p -L 10,20:src/auth/login.ts
Use Case
Git blame is a daily tool for understanding unfamiliar code. When encountering a confusing line, blame reveals who wrote it and when, making it easy to ask the right person or find the associated PR for context. The -C flag is particularly valuable in refactored codebases where code has moved between files. The .git-blame-ignore-revs feature is increasingly adopted by teams that run formatters, ensuring blame output stays useful after bulk formatting commits.