0
0
Gitdevops~5 mins

Searching history with git log -S - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to find when a specific piece of code was added or removed in your project. The git log -S command helps you search the commit history for changes that added or removed a specific string.
When you want to find the commit that introduced a bug by searching for a changed function name.
When you need to track when a specific configuration value was added or removed in your code.
When you want to see all commits that added or removed a particular variable or keyword.
When you are investigating why a certain line of code changed over time.
When you want to understand the history of a feature by searching for its name in commit changes.
Commands
This command searches the commit history for any commits that added or removed the string 'initializeApp'. The --oneline flag shows each commit in a short, easy-to-read format.
Terminal
git log -S'initializeApp' --oneline
Expected OutputExpected
a1b2c3d Add initializeApp function f4e5d6c Remove old initializeApp call
-S - Search commits that add or remove the specified string
--oneline - Show each commit in a single line summary
This command shows the patch (code changes) for the last 2 commits that added or removed 'initializeApp'. It helps you see exactly what changed in those commits.
Terminal
git log -S'initializeApp' -p -2
Expected OutputExpected
commit a1b2c3d4e5f6g7h8i9j0 Author: Jane Doe <jane@example.com> Date: Mon Apr 1 12:00:00 2024 +0000 Add initializeApp function diff --git a/app.js b/app.js index 1234567..89abcde 100644 --- a/app.js +++ b/app.js @@ -10,6 +10,10 @@ +function initializeApp() { + console.log('App initialized'); +} commit f4e5d6c7b8a9c0d1e2f3 Author: John Smith <john@example.com> Date: Sun Mar 31 15:30:00 2024 +0000 Remove old initializeApp call diff --git a/app.js b/app.js index 89abcde..1234567 100644 --- a/app.js +++ b/app.js @@ -20,7 +20,6 @@ - initializeApp();
-p - Show the patch (code changes) introduced by each commit
-2 - Limit output to the last 2 commits
Key Concept

If you remember nothing else from this pattern, remember: git log -S lets you find commits that added or removed a specific string in your code history.

Common Mistakes
Using git log -S without quotes around the search string when it contains special characters or spaces
The shell may misinterpret the string, causing the command to fail or give wrong results
Always put the search string in single quotes like -S'initializeApp' to ensure it is treated as one argument
Confusing -S with -G which searches by regex pattern instead of exact string changes
-G searches commit diffs for matching regex patterns, which can give different results than -S
Use -S when you want to find commits that add or remove an exact string, and -G for regex pattern searches
Summary
Use git log -S'string' to find commits that added or removed that exact string.
Add --oneline for a simple summary of matching commits.
Use -p to see the code changes in those commits.
Limit results with flags like -2 to see only recent matches.