Interactive rebase (git rebase -i) - Time & Space Complexity
When using interactive rebase in Git, it is important to understand how the time it takes grows as you work with more commits.
We want to know how the number of commits affects the work Git does during rebase.
Analyze the time complexity of the following git interactive rebase command.
git rebase -i HEAD~5
# This opens an editor to reorder, squash, or edit the last 5 commits.
# Git will then replay these commits one by one on top of the base commit.
This command lets you change the last 5 commits by replaying them interactively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Git replays each commit one by one during the rebase.
- How many times: Once for each commit in the range (here, 5 times).
As the number of commits to rebase increases, Git must replay more commits, so the work grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Replays 10 commits |
| 100 | Replays 100 commits |
| 1000 | Replays 1000 commits |
Pattern observation: Doubling the commits roughly doubles the work Git does during rebase.
Time Complexity: O(n)
This means the time to complete the interactive rebase grows directly with the number of commits you want to change.
[X] Wrong: "Interactive rebase time stays the same no matter how many commits I edit."
[OK] Correct: Each commit must be replayed, so more commits mean more work and longer time.
Understanding how operations scale with input size shows you think about efficiency, a key skill in real-world software work.
"What if we only rebase a single commit instead of many? How would the time complexity change?"