Rebase vs merge mental model in Git - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When using Git, combining changes from different branches can be done by merging or rebasing. Understanding how the time to combine grows with the number of commits helps us choose the right method.
We want to know: How does the work Git does increase as the number of commits to combine grows?
Analyze the time complexity of these Git commands:
git merge main
git rebase main
These commands combine changes from one branch into another, but in different ways.
Look at what Git does internally when running these commands:
- Primary operation: For merge, Git compares the latest commits and creates a new commit combining changes. For rebase, Git re-applies each commit from the feature branch one by one onto the main branch.
- How many times: Merge does a single combine operation regardless of commits. Rebase repeats the apply step once per commit in the feature branch.
Imagine the feature branch has different numbers of commits to combine:
| Input Size (n commits) | Approx. Operations |
|---|---|
| 10 | Merge: 1 combine, Rebase: 10 applies |
| 100 | Merge: 1 combine, Rebase: 100 applies |
| 1000 | Merge: 1 combine, Rebase: 1000 applies |
Pattern observation: Merge work stays the same no matter how many commits. Rebase work grows linearly with the number of commits.
Time Complexity: O(n)
This means the time Git takes to rebase grows directly with the number of commits, while merge time stays mostly constant.
[X] Wrong: "Rebase and merge take the same time no matter how many commits there are."
[OK] Correct: Merge combines all changes at once, but rebase applies each commit separately, so more commits mean more work for rebase.
Knowing how Git commands scale with commit count shows you understand their inner workings. This helps you explain your choices clearly and confidently in real projects or interviews.
"What if we used interactive rebase to squash commits before rebasing? How would that affect the time complexity?"
Practice
git merge and git rebase?Solution
Step 1: Understand
git mergebehaviorgit mergecombines two branches by creating a new commit that preserves the history of both branches without changing existing commits.Step 2: Understand
git rebasebehaviorgit rebasemoves or reapplies commits from one branch onto another, rewriting history to make it look like a straight line.Final Answer:
git mergecombines histories preserving all commits;git rebaserewrites history to create a linear sequence. -> Option DQuick Check:
Merge preserves history, rebase rewrites it [OK]
- Thinking merge deletes branches
- Believing rebase only works on remote branches
- Confusing which command rewrites history
main?Solution
Step 1: Identify the command to rebase current branch
The commandgit rebase mainrebases the current branch onto themainbranch.Step 2: Check other options for correctness
git merge mainmerges, not rebases;git rebase origin/mainrebases onto remote tracking branch which may be outdated;git checkout main && git rebaseis invalid syntax.Final Answer:
git rebase main -> Option AQuick Check:
Rebase current branch onto main = git rebase main [OK]
- Using merge instead of rebase
- Rebasing onto remote branch without fetching
- Incorrect chaining of commands
feature:
git checkout feature git rebase main git log --oneline --graphWhat will the commit history look like compared to using
git merge main instead?Solution
Step 1: Understand effect of
Rebasing moves feature commits to be based on the latest main commits, creating a straight, linear history.git rebase mainon feature branchStep 2: Compare with
Merging creates a new merge commit that combines histories, preserving the branch structure and showing a branch point.git merge maineffectFinal Answer:
A linear history with feature commits on top of main commits. -> Option AQuick Check:
Rebase = linear history; merge = merge commit [OK]
- Thinking rebase creates merge commits
- Believing history stays unchanged after rebase
- Assuming commits are deleted after rebase
git rebase main on your feature branch but got conflicts. After resolving conflicts, which command should you run to continue the rebase?Solution
Step 1: Identify the correct command to continue rebase after conflicts
After resolving conflicts during a rebase,git rebase --continuetells Git to proceed with applying remaining commits.Step 2: Understand other options
git merge --continueis for merge conflicts, not rebase;git commit -mis manual commit but rebase expects--continue;git rebase --abortcancels the rebase.Final Answer:
git rebase --continue -> Option CQuick Check:
Continue rebase after conflicts = git rebase --continue [OK]
- Using merge commands during rebase
- Trying to commit manually instead of continuing
- Aborting instead of continuing rebase
main but keep a clean, linear history without merge commits. Which sequence of commands achieves this safely?Solution
Step 1: Fetch latest changes from remote main branch
git fetch originupdates local remote tracking branches without changing working branches.Step 2: Rebase feature branch onto updated origin/main
git rebase origin/mainreapplies feature commits on top of latest main commits, keeping history linear and clean.Final Answer:
git checkout feature; git fetch origin; git rebase origin/main -> Option BQuick Check:
Fetch then rebase for clean update [OK]
- Merging instead of rebasing for linear history
- Pulling directly on feature branch causing merge commits
- Not fetching latest remote changes before rebase
