Bird
Raised Fist0
Gitdevops~5 mins

Searching history with git log -S - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the git log -S command do?
easy
A. Finds commits that added or removed a specific string in the code.
B. Shows the current status of files in the working directory.
C. Displays the list of branches in the repository.
D. Creates a new branch with the given name.

Solution

  1. Step 1: Understand the purpose of git log -S

    This command searches commit history for changes that added or removed a specific string.
  2. Step 2: Compare with other git commands

    Other options like git status show file status, git branch lists branches, and git branch <name> creates branches, which are different tasks.
  3. Final Answer:

    Finds commits that added or removed a specific string in the code. -> Option A
  4. Quick Check:

    Search commits by string change = B [OK]
Hint: Remember: -S searches for string changes in commits [OK]
Common Mistakes:
  • Confusing -S with showing file status
  • Thinking it lists branches
  • Assuming it creates branches
2. Which of the following is the correct syntax to find commits that added or removed the word fix using git log -S?
easy
A. git log -S fix
B. git log -s fix
C. git log --search=fix
D. git log -search fix

Solution

  1. Step 1: Identify correct option flag

    The correct flag to search for string changes is uppercase -S, so git log -S fix is correct.
  2. Step 2: Check other options for syntax errors

    -s is not valid for this purpose, and --search or -search are not valid git log options.
  3. Final Answer:

    git log -S fix -> Option A
  4. Quick Check:

    Uppercase -S for string search = A [OK]
Hint: Use uppercase -S to search string changes in git log [OK]
Common Mistakes:
  • Using lowercase -s instead of -S
  • Trying non-existent --search option
  • Adding extra dashes incorrectly
3. Given the following git log command:
git log -S 'bugfix' --oneline
What will this command output?
medium
A. An error because the string 'bugfix' is not quoted correctly.
B. A list of all commits with the word 'bugfix' anywhere in the commit message.
C. A list of commits that modified files named 'bugfix'.
D. A list of commits that added or removed the string 'bugfix', shown in one line each.

Solution

  1. Step 1: Understand -S 'bugfix' usage

    This searches commits that added or removed the exact string 'bugfix' in the code or content.
  2. Step 2: Understand --oneline option

    This shows each commit in a short single line format for easy reading.
  3. Final Answer:

    A list of commits that added or removed the string 'bugfix', shown in one line each. -> Option D
  4. Quick Check:

    -S finds string changes, --oneline shortens output = D [OK]
Hint: Combine -S with --oneline for short commit list by string [OK]
Common Mistakes:
  • Thinking it searches commit messages instead of code changes
  • Assuming it filters by file names
  • Believing quotes cause errors here
4. You run git log -S 'update' but get no results, even though you know the word 'update' was added in some commits. What could be the problem?
medium
A. You need to use git log -G 'update' to search commit messages.
B. You forgot to put quotes around the search string.
C. The string 'update' was only changed in commit messages, not in code.
D. The repository has no commits at all.

Solution

  1. Step 1: Understand what -S searches

    -S searches for string changes in the code or content, not in commit messages.
  2. Step 2: Consider commit message search

    If 'update' was only added or changed in commit messages, -S won't find it; use git log --grep 'update' to search commit messages. -G searches code diffs by regex.
  3. Final Answer:

    The string 'update' was only changed in commit messages, not in code. -> Option C
  4. Quick Check:

    -S searches code changes, not commit messages = A [OK]
Hint: Use -G to search commit messages, -S for code changes [OK]
Common Mistakes:
  • Assuming -S searches commit messages
  • Thinking quotes cause no results
  • Believing repository is empty without checking
5. You want to find all commits that added or removed the string TODO but only in the src/ folder. Which command will do this correctly?
hard
A. git log -S 'TODO' --path src/
B. git log -S 'TODO' -- src/
C. git log -S 'TODO' src/
D. git log --S 'TODO' -- src/

Solution

  1. Step 1: Use -S 'TODO' to search string changes

    This finds commits adding or removing 'TODO'.
  2. Step 2: Use -- src/ to limit search to the src folder

    The double dash -- separates options from path arguments, so -- src/ limits the search to that folder.
  3. Step 3: Check other options for syntax correctness

    src/ alone without -- is invalid here; --path is not a git log option; --S is invalid (uppercase S must be after git log).
  4. Final Answer:

    git log -S 'TODO' -- src/ -> Option B
  5. Quick Check:

    Use -- before path to limit git log search = C [OK]
Hint: Use -- before folder path to limit git log search [OK]
Common Mistakes:
  • Omitting -- before path
  • Using invalid --path option
  • Writing --S instead of -S