KKAIROS
Practical guideIllustrative3 min read

Git Blame: who last changed each line

git blame FILE prints, for every line, the commit that last changed it. -L START,END limits the output to a line range, and -L START,+COUNT limits it to COUNT lines from START.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-60-A: blame -L attributes a single line to the commit that last changed it

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\nb\n' > f.txt; git add f.txt; git commit -qm first
printf 'a\nB\n' > f.txt; git commit -qam second

Command:

git blame -L 1,1 --porcelain f.txt | grep '^summary'; git blame -L 2,2 --porcelain f.txt | grep '^summary'

Recorded output:

summary first
summary second

Exit status: 0. The output matches the expected text written before the run. This is the status of grep, the last command in the pipe.

TE-60-B: blame -L with a start and a line count (2,+1) attributes the second line to its commit

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\nb\n' > f.txt; git add f.txt; git commit -qm first
printf 'a\nB\n' > f.txt; git commit -qam second

Command:

git blame -L 2,+1 --porcelain f.txt | grep '^summary'

Recorded output:

summary second

Exit status: 0. This status and the output match what was written before the run. The status is that of grep, the last command in the pipe.

TE-60-C: expected non-zero exit: git blame on a path that is not in HEAD stops with a fatal error

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one

Command:

git blame nosuch.txt

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: no such path 'nosuch.txt' in HEAD

Exit status: 128. This status and the output match what was written before the run.

TE-60-B: by the setup, the second line was changed by the commit named second. -L 2,+1 selected one line starting at line 2, and the summary line reads summary second. TE-60-C: git blame nosuch.txt exited with status 128, and standard error reads fatal: no such path 'nosuch.txt' in HEAD.

Limits

Blame names the last commit that touched a line, which may not be the commit that introduced the idea. Moved or copied lines (-M, -C) were not tested.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-60-A | blame -L attributes a single line to the commit that last changed it | 0 | PASS | 3176f379c4e1 | | TE-60-B | blame -L with a start and a line count (2,+1) attributes the second line to its commit | 0 | PASS | dfdc9dee5570 | | TE-60-C | expected non-zero exit: git blame on a path that is not in HEAD stops with a fatal error | 128 | PASS | be922f84c927 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides