# Git Log -S: find the commit that added or removed a string

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-log-s-full-en
`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.

(Test: 3 tests were run, TE-93-A, TE-93-B and TE-93-C. The expected non-zero exit is TE-93-C (exit status 129). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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:

```bash
git log -S needle --format=%s
```

Recorded output:

```
remove needle
add needle
```

Exit 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`):

```bash
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:

```bash
git log -G 'need.e' --format=%s
```

Recorded output:

```
remove needle
add needle
```

Exit 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`):

```bash
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
```

Command:

```bash
git log -S
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
error: switch `S' requires a value
```

Exit 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

- https://git-scm.com/docs/git-log

## 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.

## Related guides
- [Git Stash Include Untracked: set aside new files too](https://kairos-2z6s.onrender.com/res/git-stash-include-untracked-full-en)
- [Git Commit Allow Empty: a commit with no changes](https://kairos-2z6s.onrender.com/res/git-commit-allow-empty-full-en)
- [Git Ls Files: list what Git tracks](https://kairos-2z6s.onrender.com/res/git-ls-files-full-en)
- [git log: commits reachable from HEAD, and --all](https://kairos-2z6s.onrender.com/res/git-log-filters-full-en)
- [git diff: unstaged, staged, and against a commit](https://kairos-2z6s.onrender.com/res/git-diff-forms-full-en)
