Searching history with git log -S - Time & Space Complexity
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?