git blame Command Guide
Show what revision and author last modified each line of a file
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git blame <file>Blame entire filegit blame -L 10,20 <file>Blame lines 10-20git blame -e <file>Show email addressesOptions
-wIgnore whitespace-MDetect moved lines-CDetect copied linesFormat
-sSuppress author name--date=shortShort date format--porcelainMachine-readableDownloadable Image Preview
Basic Usage
The git blame command shows who last modified each line of a file. It's useful for understanding code history and finding the right person to ask about specific code.
# Blame an entire file
git blame src/app.js
# Output shows:
# commit-hash (author date time line-number) content
abc1234 (John Doe 2024-01-15 10:30 1) const app = {
def5678 (Jane Smith 2024-02-20 14:45 2) name: 'MyApp',Blaming Line Ranges
# Blame specific line range
git blame -L 10,20 src/app.js
# Blame from line 10 to end
git blame -L 10, src/app.js
# Blame 5 lines starting at line 10
git blame -L 10,+5 src/app.js
# Blame function (if supported)
git blame -L :functionName src/app.jsCommon Options
| Option | Description |
|---|---|
| -L <start>,<end> | Annotate only the line range |
| -e | Show author email instead of name |
| -w | Ignore whitespace changes |
| -M | Detect moved lines within file |
| -C | Detect lines copied from other files |
| -s | Suppress author name/time |
| --date=short | Show short date format |
Tracking Code Movement
# Detect moved/renamed lines within file
git blame -M src/app.js
# Detect lines moved from other files
git blame -C src/app.js
# More aggressive copy detection
git blame -C -C -C src/app.js
# Ignore whitespace changes
git blame -w src/app.js
# Combine: ignore whitespace, detect moves/copies
git blame -w -M -C src/app.jsCopy Detection Levels
-C- Detect copies from modified files in same commit-C -C- Also look at commits that created the file-C -C -C- Look at all files in any commit
Practical Examples
# Find who wrote a specific function
git blame -L :handleClick src/button.js
# Compact blame output
git blame -s --date=short src/app.js
# Blame at a specific commit
git blame v1.0.0 -- src/app.js
# Find original author (ignoring refactoring)
git blame -w -M -C src/app.js
# Get emails for contacting authors
git blame -e src/app.js
# Machine-readable output for scripts
git blame --porcelain src/app.jsFrequently Asked Questions
How do I blame a specific line range?
Use "git blame -L <start>,<end> <file>". For example: "git blame -L 10,20 app.js" shows blame for lines 10-20.
How do I find the original author of moved/copied code?
Use "git blame -M" to detect moved lines within a file, or "git blame -C" to detect lines copied from other files.
How do I ignore whitespace changes in blame?
Use "git blame -w" to ignore whitespace when comparing commits. This helps find the actual content change, not just reformatting.
Can I blame a file at a specific commit?
Yes! Use "git blame <commit> -- <file>" to see blame as of that commit. This is useful for investigating historical states.
How do I see the full commit message for a blame line?
First get the commit hash from blame output, then use "git show <hash>" or "git log -1 <hash>" to see the full commit details.
Summary
git blame helps you understand code history and find the right person to discuss specific changes with.
Quick Reference
git blame <file>- Blame entire filegit blame -L 10,20- Blame line rangegit blame -w- Ignore whitespacegit blame -M -C- Detect moved/copied code
Official Documentation
For authoritative information, refer to the official documentation: