Searching history with git log -S - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we search for changes in a Git repository using git log -S, we want to know how the time it takes grows as the project history grows.
We ask: How does searching for a string in commit history scale with more commits?
Analyze the time complexity of this Git command:
git log -S "search_string" --source --all
This command searches all commits for changes that add or remove the exact string "search_string".
Look at what repeats when running this command:
- Primary operation: Git examines each commit's changes to find the string.
- How many times: Once for every commit in the repository history.
As the number of commits grows, Git checks each commit one by one for the string.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 commits | 10 checks |
| 100 commits | 100 checks |
| 1000 commits | 1000 checks |
Pattern observation: The work grows directly with the number of commits.
Time Complexity: O(n)
This means the time to search grows in a straight line as the commit count increases.
[X] Wrong: "Git searches only the latest commits, so time stays the same no matter how many commits exist."
[OK] Correct: Git actually checks every commit in the history to find changes with the string, so more commits mean more work.
Understanding how Git searches history helps you explain how tools handle large data and why some commands slow down as projects grow. This skill shows you think about efficiency in real work.
What if we limited the search to a single branch instead of all commits? How would the time complexity change?
Practice
git log -S command do?Solution
Step 1: Understand the purpose of
This command searches commit history for changes that added or removed a specific string.git log -SStep 2: Compare with other git commands
Other options likegit statusshow file status,git branchlists branches, andgit branch <name>creates branches, which are different tasks.Final Answer:
Finds commits that added or removed a specific string in the code. -> Option AQuick Check:
Search commits by string change = B [OK]
- Confusing -S with showing file status
- Thinking it lists branches
- Assuming it creates branches
fix using git log -S?Solution
Step 1: Identify correct option flag
The correct flag to search for string changes is uppercase-S, sogit log -S fixis correct.Step 2: Check other options for syntax errors
-sis not valid for this purpose, and--searchor-searchare not valid git log options.Final Answer:
git log -S fix -> Option AQuick Check:
Uppercase -S for string search = A [OK]
- Using lowercase -s instead of -S
- Trying non-existent --search option
- Adding extra dashes incorrectly
git log -S 'bugfix' --onelineWhat will this command output?
Solution
Step 1: Understand
This searches commits that added or removed the exact string 'bugfix' in the code or content.-S 'bugfix'usageStep 2: Understand
This shows each commit in a short single line format for easy reading.--onelineoptionFinal Answer:
A list of commits that added or removed the string 'bugfix', shown in one line each. -> Option DQuick Check:
-S finds string changes, --oneline shortens output = D [OK]
- Thinking it searches commit messages instead of code changes
- Assuming it filters by file names
- Believing quotes cause errors here
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?Solution
Step 1: Understand what
-Ssearches-Ssearches for string changes in the code or content, not in commit messages.Step 2: Consider commit message search
If 'update' was only added or changed in commit messages,-Swon't find it; usegit log --grep 'update'to search commit messages.-Gsearches code diffs by regex.Final Answer:
The string 'update' was only changed in commit messages, not in code. -> Option CQuick Check:
-S searches code changes, not commit messages = A [OK]
- Assuming -S searches commit messages
- Thinking quotes cause no results
- Believing repository is empty without checking
TODO but only in the src/ folder. Which command will do this correctly?Solution
Step 1: Use
This finds commits adding or removing 'TODO'.-S 'TODO'to search string changesStep 2: Use
The double dash-- src/to limit search to the src folder--separates options from path arguments, so-- src/limits the search to that folder.Step 3: Check other options for syntax correctness
src/alone without--is invalid here;--pathis not a git log option;--Sis invalid (uppercase S must be after git log).Final Answer:
git log -S 'TODO' -- src/ -> Option BQuick Check:
Use -- before path to limit git log search = C [OK]
- Omitting -- before path
- Using invalid --path option
- Writing --S instead of -S
