git blame

Debugging

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>
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (2)

Only annotate the given line range, e.g. -L 10,20 (or a function with -L :name).

RecommendedFocus on the lines you are investigating

Example

git blame -L 12,14 app.js

Show the author email instead of the name.

When neededShow author emails when names are ambiguous

Example

git blame -e app.js

Advanced (1)

Pretend a commit (e.g. a big reformat) did not change anything; blame the lines to earlier commits.

When neededSkip formatting-only commits

Example

git blame --ignore-rev a1b2c3d app.js

Choosing the Right Option

Finding where a change came from

Three tools, three questions

CommandAnswers
git blame fileWhich commit last touched each line?
git log -p fileEvery change ever made to the file
git log -S "text"Which commit added or removed this text?

Examples

Blame a file

git blame app.js

Every line with its last commit, author and date.


A range of lines

git blame -L 10,20 app.js

Only lines 10 to 20.


An older version

git blame v1.0 -- app.js

Blame the file as it was at tag v1.0.

Workflow Examples

1
git blame -L 40,45 app.js

Find the commit of the line

2
git show a1b2c3d

Read 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

A:

The line comes from the first commit of the history: blame could not go further back.

A:

A line you changed in your working copy but have not committed.