Reflog for finding lost commits in Git - Time & Space Complexity
When using git reflog to find lost commits, we want to know how long it takes to search through the history of changes.
We ask: How does the time to find a commit grow as the number of reflog entries increases?
Analyze the time complexity of this git reflog command sequence.
git reflog
# Lists all recent HEAD changes
git show HEAD@{5}
# Shows the commit at position 5 in reflog
# Searching reflog for a commit by message or hash
git reflog | grep 'fix bug'
This code lists reflog entries and searches for a specific commit by scanning the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each reflog entry one by one.
- How many times: Once for each reflog entry, from the newest to the oldest.
As the number of reflog entries grows, the time to scan them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 scans |
| 100 | 100 scans |
| 1000 | 1000 scans |
Pattern observation: The time grows directly with the number of reflog entries.
Time Complexity: O(n)
This means the time to find a commit grows linearly with the number of reflog entries.
[X] Wrong: "Searching reflog is instant no matter how many entries there are."
[OK] Correct: Each reflog entry must be checked one by one, so more entries mean more time.
Understanding how git commands scale helps you explain your problem-solving skills clearly and confidently.
"What if git stored reflog entries in a database with indexes? How would the time complexity change?"