git log -S <string>
This command is useful for tracking down when a particular string was introduced or removed in a repository, helping you quickly identify relevant changes in the project history.
It works by searching through the commit history and displaying only those commits where the specified string was added or deleted in the content of files.
You can use this to investigate the origin of a bug, feature, or configuration by searching for a function name, variable, or any text.
You can combine this with other flags like -p
to show the patch (diff) for each commit, or --author=<name>
to filter by author.
The -G <regex>
flag is similar but matches commits where a regular expression appears in the diff, not just added or removed lines. For a broader search, git log -p
shows all diffs, and git blame <file>
can help track changes to a specific line in a file.
Example:
git log -S 'hello'