Reordering commits in Git - Time & Space Complexity
When we reorder commits in git, we want to know how the time needed changes as the number of commits grows.
We ask: How does the effort to reorder commits grow when we have more commits?
Analyze the time complexity of the following git commands used to reorder commits.
git rebase -i HEAD~5
# Opens editor to reorder last 5 commits
# User changes order and saves
# Git applies commits in new order
This snippet shows an interactive rebase to reorder the last 5 commits by rewriting history.
Look for repeated steps in the rebase process.
- Primary operation: Git applies each commit one by one in the new order.
- How many times: Once for each commit being reordered (n times).
As the number of commits to reorder grows, git applies more commits sequentially.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 commit applications |
| 50 | 50 commit applications |
| 500 | 500 commit applications |
Pattern observation: The work grows directly with the number of commits to reorder.
Time Complexity: O(n)
This means the time to reorder commits grows linearly with how many commits you reorder.
[X] Wrong: "Reordering commits is instant no matter how many commits there are."
[OK] Correct: Each commit must be reapplied, so more commits mean more work and time.
Understanding how git operations scale helps you explain your workflow choices clearly and shows you know how tools behave with bigger projects.
"What if we reorder only a subset of commits instead of all recent ones? How would the time complexity change?"