Bird
Raised Fist0
Gitdevops~5 mins

When to rebase vs when to merge in Git - Performance Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Time Complexity: When to rebase vs when to merge
O(n)
Understanding Time Complexity

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?

Scenario Under Consideration

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.

Identify Repeating Operations

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
How Execution Grows With Input

As the number of commits on the feature branch grows, the work git does changes:

Input Size (n commits)Approx. Operations
10Apply or combine 10 commits
100Apply or combine 100 commits
1000Apply or combine 1000 commits

Pattern observation: The work grows roughly in direct proportion to the number of commits to process.

Final Time Complexity

Time Complexity: O(n)

This means the time to rebase or merge grows linearly with the number of commits involved.

Common Mistake

[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.

Interview Connect

Understanding how git operations scale helps you explain your workflow choices clearly and shows you know what happens behind the scenes.

Self-Check

What if we had many small commits versus one big commit? How would that affect the time complexity of rebasing?

Practice

(1/5)
1. What is the main reason to use git rebase instead of git merge?
easy
A. To delete the feature branch after merging
B. To keep all branch merge points visible in history
C. To create a clean, linear history without merge commits
D. To automatically resolve all conflicts

Solution

  1. Step 1: Understand rebase purpose

    Rebase moves commits to create a straight line history without merge commits.
  2. Step 2: Compare with merge

    Merge keeps all branch points and creates merge commits, showing full history.
  3. Final Answer:

    To create a clean, linear history without merge commits -> Option C
  4. Quick Check:

    Rebase = linear history [OK]
Hint: Rebase = linear history, Merge = full branch history [OK]
Common Mistakes:
  • Thinking merge creates linear history
  • Believing rebase deletes branches
  • Assuming rebase auto-resolves conflicts
2. Which of the following is the correct syntax to rebase your current branch onto main?
easy
A. git rebase origin/main
B. git merge main
C. git checkout main && git rebase
D. git rebase main

Solution

  1. Step 1: Identify rebase command

    The command git rebase main rebases the current branch onto main.
  2. Step 2: Check other options

    git merge main merges, not rebases; git checkout main && git rebase is incomplete; git rebase origin/main rebases onto remote branch, not local main.
  3. Final Answer:

    git rebase main -> Option D
  4. Quick Check:

    Rebase syntax = git rebase branch [OK]
Hint: Rebase current branch onto main: git rebase main [OK]
Common Mistakes:
  • Confusing merge and rebase commands
  • Using incomplete rebase syntax
  • Rebasing onto remote branch unintentionally
3. You have a feature branch with 3 commits diverged from main. After running git rebase main, what will the commit history look like?
medium
A. The 3 commits will be replayed on top of the latest main commits
B. The 3 commits will be merged into a single commit on main
C. The 3 commits will be deleted and replaced by main commits
D. The 3 commits will remain unchanged and main will be merged

Solution

  1. Step 1: Understand rebase effect on commits

    Rebase takes your commits and re-applies them on top of the target branch, here main.
  2. Step 2: Compare with merge behavior

    Merge combines histories with a merge commit; rebase rewrites history to appear linear.
  3. Final Answer:

    The 3 commits will be replayed on top of the latest main commits -> Option A
  4. Quick Check:

    Rebase = replay commits on new base [OK]
Hint: Rebase replays commits on top of target branch [OK]
Common Mistakes:
  • Thinking rebase merges commits
  • Assuming commits are deleted
  • Confusing merge and rebase results
4. You tried to rebase your branch onto main but got conflicts. What is the correct way to continue after resolving conflicts?
medium
A. Run git rebase --continue after fixing conflicts
B. Run git merge --continue after fixing conflicts
C. Run git commit to finish the rebase
D. Run git rebase --abort to keep the changes

Solution

  1. Step 1: Identify rebase conflict resolution

    After fixing conflicts during rebase, you must run git rebase --continue to proceed.
  2. Step 2: Check other options

    git merge --continue is for merge conflicts; git commit alone doesn't continue rebase; git rebase --abort cancels rebase.
  3. Final Answer:

    Run git rebase --continue after fixing conflicts -> Option A
  4. Quick Check:

    Fix conflicts + git rebase --continue [OK]
Hint: After conflict fix in rebase, run git rebase --continue [OK]
Common Mistakes:
  • Using git merge --continue during rebase
  • Running git commit instead of rebase continue
  • Aborting rebase instead of continuing
5. Your team wants to keep a clear record of all branch merges for auditing, but also wants to avoid complex conflict resolution during integration. Which strategy should you choose?
hard
A. Use git rebase to keep history linear and avoid merge commits
B. Use git merge to preserve branch history and avoid rewriting commits
C. Use git rebase and then merge to keep both histories
D. Use git cherry-pick to manually apply commits

Solution

  1. Step 1: Understand audit needs

    Keeping a clear record means preserving all branch merge points and history.
  2. 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.
  3. Final Answer:

    Use git merge to preserve branch history and avoid rewriting commits -> Option B
  4. Quick Check:

    Audit needs = merge to keep history [OK]
Hint: Preserve history and audit: choose git merge [OK]
Common Mistakes:
  • Choosing rebase when audit needs full history
  • Mixing rebase and merge without clear purpose
  • Using cherry-pick for full branch integration