Find the commit that changed some text

Read-only
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Searches history for commits that added or removed a given string, showing when and where a line of code appeared or disappeared.

gitsearchhistorylog

Parameters

Exact string to search for in added or removed lines.

Commands

List commits where the number of occurrences of the text changed

git log -S "<text>" --oneline

Show each matching commit's diff, oldest first, to see the text's origin

git log -S "<text>" -p --reverse

Verification

git log -S "<text>" --oneline

Each listed commit's diff adds or removes the searched text.

Undo

Not undoable

This recipe only reads history and changes nothing.

Pitfalls

  • -S matches occurrence-count changes; use -G with a regex to also catch moved or modified lines.
  • Add -- <path> to limit the search to one file or directory in large repositories.

Related