Golden rule of rebasing (never rebase public) in Git - Time & Space Complexity
When using git rebase, it's important to understand how the operation scales with the number of commits involved.
We want to see how the work git does grows as more commits are rebased.
Analyze the time complexity of rebasing a branch with multiple commits.
git checkout feature-branch
# feature-branch has 50 commits
git rebase main
# main has new commits since feature-branch started
This rebases the feature branch commits onto the updated main branch.
Look for repeated steps git performs during rebase.
- Primary operation: Applying each commit one by one onto the new base.
- How many times: Once for each commit in the feature branch (e.g., 50 times).
As the number of commits to rebase increases, git must apply each commit in order.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 commits | 10 apply steps |
| 100 commits | 100 apply steps |
| 1000 commits | 1000 apply steps |
Pattern observation: The work grows directly with the number of commits to rebase.
Time Complexity: O(n)
This means the time to rebase grows linearly with the number of commits being rebased.
[X] Wrong: "Rebasing a public branch is safe and has no impact on others."
[OK] Correct: Rebasing rewrites commit history, so if others use that branch, their work will conflict and cause confusion.
Understanding how git rebase scales and why rebasing public branches is risky shows you grasp both git mechanics and teamwork impact.
"What if we rebase a branch with only one commit? How does the time complexity change?"