Skip to main content
Chapter 42 of 61 · 4 sections · Page 148

Git: Blaming

Inspect line-by-line file authorship, commit timestamps, line ranges, and ignore whitespace adjustments.

Original chapter reference: Chapter 42: Blaming. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Details filename Name of the file for which details need to be checked -f Show the file name in the origin commit -e Show the author email instead of author name -w Ignore white spaces while making a comparison between child and parent's version -L start,end Show only the given line range Example: git blame -L 1,2 [filename] --show-stats Shows additional statistics at end of blame output -l Show long rev (Default: off) -t Show raw timestamp (Default: off) -reverse Walk history forward instead of backward -p, --porcelain Output for machine consumption -M Detect moved or copied lines within a file In addition to -M, detect lines moved or copied from other files that were modified in the same -C commit -h Show the help message -c Use the same output mode as git-annotate (Default: off) -n Show the line number in the original commit (Default: off)

Section 42.1: Only show certain lines

Output can be restricted by specifying line ranges as

Source example · PDF page 148
git blame -L <start>,<end>

Where <start> and <end> can be:

line number

Source example · PDF page 148
     git blame -L 10,30

/regex/

Source example · PDF page 148
     git blame -L /void main/,git blame -L 46,/void foo/

+offset, -offset (only for <end>)

Source example · PDF page 148
     git blame -L 108,+30,git blame -L 215,-15

Multiple line ranges can be specified, and overlapping ranges are allowed.

Source example · PDF page 148
git blame -L 10,30 -L 12,80 -L 120,+10 -L ^/void main/,+40

Section 42.2: To find out who changed a file

Source example · PDF page 148
// Shows the author and commit per line of specified file
git blame test.c
// Shows the author email and commit per line of specified
git blame -e test.c file

// Limits the selection of lines by specified range
git blame -L 1,10 test.c

Section 42.3: Show the commit that last modified a line

Source example · PDF page 149
git blame <file>

will show the file with each line annotated with the commit that last modified it.

Section 42.4: Ignore whitespace-only changes

Sometimes repos will have commits that only adjust whitespace, for example fixing indentation or switching between tabs and spaces. This makes it difficult to find the commit where the code was actually written.

Source example · PDF page 149
git blame -w

will ignore whitespace-only changes to find where the line really came from.

Buy me a coffee
Daily Practice
0/0