Git Reference ยท Inspecting History
git blame command
Reference for git blame: annotate every line with the commit and author that last changed it, limit to line ranges, ignore whitespace and reformatting commits, and script with porcelain output.
git blame prints a file with each line annotated by the commit that last changed it, together with the author and date. It answers the question "why does this line look like this?" by pointing you at the commit message that explains it, which you can then open with git show.
Despite the name, it is a history tool, not a way to assign guilt. Its real power is in the options: you can restrict the output to a few lines, ignore whitespace-only changes, follow lines that were moved or copied from other files, and skip specific commits (such as a mass reformatting) so that blame points to the meaningful change.
git blame <file> git blame -L 10,20 <file> git blame <revision> -- <file>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git blame [-L <range>] [-w] [-M[<num>]] [-C[<num>]] [--ignore-rev <rev>] [--ignore-revs-file <file>] [-s | -e] [--date=<format>] [--porcelain | --line-porcelain | --incremental] [<rev>] [--] <file>
| Option | Description |
|---|---|
-L <start>,<end> |
Annotate only a range. <start> and <end> can be line numbers, /regex/, or +N/-N relative offsets (-L 40,+10). Can be repeated. -L :<funcname> blames one function. |
<rev> |
Start from this revision instead of HEAD. With --reverse <old>..<new>, blame instead shows the last commit in which each line still existed. |
-w |
Ignore whitespace when comparing the parent and child. |
-M[<num>] |
Detect lines moved within a file. |
-C[<num>] |
Detect lines moved or copied from other files in the same commit. Repeat (-C -C) to look in the commit that created the file, and again (-C -C -C) in any commit. |
--ignore-rev <rev> |
Skip a commit when blaming; its lines are attributed to the previous change. Can be repeated. |
--ignore-revs-file <file> |
Read commit IDs to ignore, one per line (# comments allowed). The blame.ignoreRevsFile setting does this by default. |
-s |
Suppress author name and timestamp. |
-e, --show-email |
Show the author email instead of the name. |
-l |
Show the full commit ID. |
-f, --show-name |
Show the file name in the original commit (useful with -C or renames). |
-n, --show-number |
Show the original line number. |
--date=<format> |
Format of the date column (short, iso, relative, ...). |
--porcelain, --line-porcelain, --incremental |
Machine-readable output. Porcelain prints commit details only the first time a commit appears; line-porcelain repeats them for every line. |
--since=<date> |
Stop at commits older than this; their lines show as boundary commits. |
Output. Each line reads <short id> (<author> <date> <line number>) <content>. A leading ^ before the ID marks a boundary commit, the oldest commit the search reached (often the root commit).
Usage notes
- Blame shows the last change, not the origin. A reformat or rename takes over every line it touches. Use
-w,--ignore-revor a.git-blame-ignore-revsfile (see .git-blame-ignore-revs) to see through them. - Renames of the whole file are followed automatically; moves of single lines between files need
-C. - It is slower than
git logon large files with long histories.-Lmakes it much faster, because Git only tracks those lines. - Combine with
git log -L(see git log) to see the whole story of a range of lines instead of the last change only. - Uncommitted changes are shown with the ID
00000000and the author "Not Committed Yet". - Merge commits are attributed to the parent that supplied the line; blame walks the first parent preferentially, so squash merges hide the original authors.
blame.markIgnoredLinesandblame.markUnblamableLinesmark lines whose attribution was uncertain because of ignored commits.- In scripts, parse
--porcelainor--line-porcelain; the default column layout can change with options and terminal width.