git blame
Show who last changed each line of a file
The 'git blame' command prints a file with, for every line, the commit that last changed it, its author and its date. It is the fastest way to find out why a line looks the way it does: take the hash, then read the full commit with git show. Lines you have not committed yet are marked 'Not Committed Yet'. Use -L to look at a few lines only.
Synopsis
git blame [<options>] [<rev>] [--] <file>Options
Choosing the Right Option
Finding where a change came from
Three tools, three questions
| Command | Answers |
|---|---|
git blame file | Which commit last touched each line? |
git log -p file | Every change ever made to the file |
git log -S "text" | Which commit added or removed this text? |
Examples
Blame a file
git blame app.jsEvery line with its last commit, author and date.
A range of lines
git blame -L 10,20 app.jsOnly lines 10 to 20.
An older version
git blame v1.0 -- app.jsBlame the file as it was at tag v1.0.
Workflow Examples
git blame -L 40,45 app.jsFind the commit of the line
git show a1b2c3dRead the whole commit and its message
Troubleshooting
fatal: no such path 'x' in HEAD
Cause: The file is not tracked, or the path is wrong
Commit the file first, or check the path
Every line points to one reformatting commit
Cause: Blame shows the LAST change
Use --ignore-rev <commit>, or blame the commit before it
Tips & Pitfalls
Best Practices
- ✓Read the commit (git show) before drawing conclusions from blame.
- ✓Keep a .git-blame-ignore-revs file listing pure formatting commits.
Common Mistakes
- ✗Treating blame as blame: the last person to touch a line may only have reformatted it.
- ✗Stopping at a formatting commit instead of blaming its parent (git blame <hash>^ -- file).
- ✗Forgetting -L on big files and scrolling through thousands of lines.
FAQ
The line comes from the first commit of the history: blame could not go further back.
A line you changed in your working copy but have not committed.