Git Log -S: find the commit that added or removed a string
git log -S STRING lists the commits that changed how many times STRING appears in a file. The run uses one fixed string.
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-93-A: log -S lists the commit that added the string and the commit that removed it, and leaves out an unrelated 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\n' > f.txt; git add f.txt; git commit -qm one
printf 'a\nneedle\n' > f.txt; git commit -qam 'add needle'
printf 'a\nneedle\nb\n' > f.txt; git commit -qam 'add b'
printf 'a\nb\n' > f.txt; git commit -qam 'remove needle'Command:
git log -S needle --format=%sRecorded output:
remove needle
add needleExit status: 0. The output matches the expected text written before the run.
TE-93-B: git log -G with a regular expression lists the commits whose diff has a matching line
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\n' > f.txt; git add f.txt; git commit -qm one
printf 'a\nneedle\n' > f.txt; git commit -qam 'add needle'
printf 'a\nneedle\nb\n' > f.txt; git commit -qam 'add b'
printf 'a\nb\n' > f.txt; git commit -qam 'remove needle'Command:
git log -G 'need.e' --format=%sRecorded output:
remove needle
add needleExit status: 0. This status and the output match what was written before the run.
TE-93-C: expected non-zero exit: git log -S without a string prints a usage error and exits with status 129
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\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git log -SRecorded output: none, standard output was empty.
Standard error from the same run:
error: switch `S' requires a valueExit status: 129. This status and the output match what was written before the run.
TE-93-B: -G 'need.e' listed remove needle and add needle, newest first. Those are the two commits whose diff has a line that matches the expression. TE-93-C: git log -S with no string exited with status 129, and standard error reads error: switch `S' requires a value.
Limits
The run uses one fixed string. TE-93-B runs -G with one regular expression. Not tested on this page: --decorate-refs and --decorate-refs-exclude (decorate only the refs that match a pattern, or leave out the ones that match), --decorate and -L (trace the evolution of a line range or of a function in a file). The names and descriptions come from the usage text printed by git log -h in git 2.34.1. No run here used them.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-93-A | log -S lists the commit that added the string and the commit that removed it, and leaves out an unrelated commit | 0 | PASS | 5c2b46051a74 | | TE-93-B | git log -G with a regular expression lists the commits whose diff has a matching line | 0 | PASS | 52c763a6e0fc | | TE-93-C | expected non-zero exit: git log -S without a string prints a usage error and exits with status 129 | 129 | PASS | 514186855bfb |
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