Recovering deleted branches in Git - Time & Space Complexity
When recovering deleted branches in git, we want to know how the time to find and restore a branch changes as the repository grows.
How does git search through its history to recover a branch?
Analyze the time complexity of this git command sequence.
# Find the commit hash of the deleted branch
git reflog
# Create a new branch at that commit
git branch recovered-branch <commit-hash>
This code finds the commit where the deleted branch pointed and recreates the branch there.
Look for repeated steps that take time as input grows.
- Primary operation: Scanning the reflog entries to find the commit hash.
- How many times: Once per reflog entry, which grows with the number of recent git actions.
As the number of reflog entries grows, git scans more entries to find the commit.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reflog entries scanned |
| 100 | About 100 reflog entries scanned |
| 1000 | About 1000 reflog entries scanned |
Pattern observation: The time grows roughly in direct proportion to the number of reflog entries.
Time Complexity: O(n)
This means the time to recover a deleted branch grows linearly with the number of reflog entries git must check.
[X] Wrong: "Recovering a deleted branch is instant no matter how big the repo is."
[OK] Correct: Git must scan reflog entries one by one, so more history means more time to find the right commit.
Understanding how git searches history helps you reason about performance in real projects and shows you can think about tool behavior beyond just commands.
What if git stored reflog entries in a database with indexes? How would that change the time complexity?