When to rebase vs when to merge in Git - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to combine changes in git grows as the number of commits increases.
Specifically, when using rebase or merge, how does the work scale?
Analyze the time complexity of these git commands:
git checkout feature-branch
git rebase main
# or alternatively
git checkout main
git merge feature-branch
This code shows rebasing a feature branch onto main, or merging a feature branch into main.
Look at what git does internally when rebasing or merging:
- Primary operation: Applying commits one by one (rebase) or combining commit trees (merge)
- How many times: Number of commits on the feature branch since it diverged from main
As the number of commits on the feature branch grows, the work git does changes:
| Input Size (n commits) | Approx. Operations |
|---|---|
| 10 | Apply or combine 10 commits |
| 100 | Apply or combine 100 commits |
| 1000 | Apply or combine 1000 commits |
Pattern observation: The work grows roughly in direct proportion to the number of commits to process.
Time Complexity: O(n)
This means the time to rebase or merge grows linearly with the number of commits involved.
[X] Wrong: "Rebasing or merging always takes the same time regardless of commits."
[OK] Correct: More commits mean more changes to apply or combine, so it takes longer.
Understanding how git operations scale helps you explain your workflow choices clearly and shows you know what happens behind the scenes.
What if we had many small commits versus one big commit? How would that affect the time complexity of rebasing?
Practice
git rebase instead of git merge?Solution
Step 1: Understand rebase purpose
Rebase moves commits to create a straight line history without merge commits.Step 2: Compare with merge
Merge keeps all branch points and creates merge commits, showing full history.Final Answer:
To create a clean, linear history without merge commits -> Option CQuick Check:
Rebase = linear history [OK]
- Thinking merge creates linear history
- Believing rebase deletes branches
- Assuming rebase auto-resolves conflicts
main?Solution
Step 1: Identify rebase command
The commandgit rebase mainrebases the current branch ontomain.Step 2: Check other options
git merge mainmerges, not rebases;git checkout main && git rebaseis incomplete;git rebase origin/mainrebases onto remote branch, not localmain.Final Answer:
git rebase main -> Option DQuick Check:
Rebase syntax = git rebase branch [OK]
- Confusing merge and rebase commands
- Using incomplete rebase syntax
- Rebasing onto remote branch unintentionally
main. After running git rebase main, what will the commit history look like?Solution
Step 1: Understand rebase effect on commits
Rebase takes your commits and re-applies them on top of the target branch, heremain.Step 2: Compare with merge behavior
Merge combines histories with a merge commit; rebase rewrites history to appear linear.Final Answer:
The 3 commits will be replayed on top of the latestmaincommits -> Option AQuick Check:
Rebase = replay commits on new base [OK]
- Thinking rebase merges commits
- Assuming commits are deleted
- Confusing merge and rebase results
main but got conflicts. What is the correct way to continue after resolving conflicts?Solution
Step 1: Identify rebase conflict resolution
After fixing conflicts during rebase, you must rungit rebase --continueto proceed.Step 2: Check other options
git merge --continueis for merge conflicts;git commitalone doesn't continue rebase;git rebase --abortcancels rebase.Final Answer:
Run git rebase --continue after fixing conflicts -> Option AQuick Check:
Fix conflicts + git rebase --continue [OK]
- Using git merge --continue during rebase
- Running git commit instead of rebase continue
- Aborting rebase instead of continuing
Solution
Step 1: Understand audit needs
Keeping a clear record means preserving all branch merge points and history.Step 2: Compare merge and rebase for conflicts
Merge preserves history and avoids rewriting commits, reducing conflict complexity; rebase rewrites history and can cause conflicts.Final Answer:
Usegit mergeto preserve branch history and avoid rewriting commits -> Option BQuick Check:
Audit needs = merge to keep history [OK]
- Choosing rebase when audit needs full history
- Mixing rebase and merge without clear purpose
- Using cherry-pick for full branch integration
