Golden rule of rebasing (never rebase public) in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
never rebase public?Solution
Step 1: Understand what rebasing does
Rebasing rewrites commit history by moving commits to a new base.Step 2: Consider the effect on public branches
If you rebase a branch others use, their history conflicts with the rewritten one, causing confusion and errors.Final Answer:
Rebasing public branches can rewrite shared history and confuse collaborators. -> Option BQuick Check:
Rebasing public = rewrite shared history = confusion [OK]
- Thinking rebasing speeds cloning
- Believing rebasing auto-resolves conflicts
- Assuming rebasing deletes commits permanently
main?Solution
Step 1: Recall basic rebase syntax
The command to rebase the current branch onto another isgit rebase <branch>.Step 2: Check options given
Onlygit rebase mainmatches the correct syntax to rebase ontomain.Final Answer:
git rebase main -> Option AQuick Check:
Basic rebase syntax = git rebase branch [OK]
- Adding unnecessary flags like -m or --merge
- Using --onto incorrectly without extra arguments
- Confusing rebase with merge commands
feature that you rebased onto main. What happens if you try to push it to a remote where feature was already shared without force?Solution
Step 1: Understand rebasing effect on commit history
Rebasing rewrites commits, so local branch history differs from remote.Step 2: Consider git push behavior
Git refuses to push if histories diverge to prevent overwriting others' work unless forced.Final Answer:
Push is rejected because history has diverged. -> Option DQuick Check:
Rebase + push without force = rejected [OK]
- Assuming push overwrites remote without force
- Thinking push merges automatically
- Believing push deletes remote branch
Solution
Step 1: Understand the problem caused by rebasing public branch
Rebasing rewrites history, so collaborators' copies conflict with the new history.Step 2: Fix by force pushing and coordinating with collaborators
Force push updates remote with new history; collaborators must rebase or reset to sync.Final Answer:
Force push the rebased branch and ask collaborators to rebase or reset. -> Option CQuick Check:
Fix rebase public = force push + collaborator reset [OK]
- Expecting collaborators to fix without reset
- Deleting remote branch unnecessarily
- Merging rebased branch to fix history
feature is already pushed and shared. What is the safest workflow to update your branch without breaking the golden rule?Solution
Step 1: Avoid rebasing a shared branch directly
Rebasing shared branches breaks history for others, so avoid it.Step 2: Use a new local branch and cherry-pick commits
Create a fresh branch frommain, apply your commits cleanly, then push as new branch to avoid rewriting shared history.Final Answer:
Create a new local branch from main, cherry-pick your commits, then push as a new branch. -> Option AQuick Check:
Keep history clean = new branch + cherry-pick + push new [OK]
- Force pushing rebased shared branch
- Merging instead of rebasing when clean history needed
- Deleting remote branch unnecessarily
