git rebase basic usage - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using git rebase, it's helpful to know how the time it takes grows as the number of commits increases.
We want to understand how the work done by rebase changes when there are more commits to process.
Analyze the time complexity of the following git rebase command.
git checkout feature-branch
# Switch to the feature branch
git rebase main
# Replay feature commits on top of main branch
This code moves the feature branch commits to start after the latest commit on main.
Look for repeated work done during the rebase process.
- Primary operation: Applying each commit from the feature branch one by one onto the main branch.
- How many times: Once for each commit in the feature branch that is not in main.
As the number of commits to rebase increases, the work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 commits | About 10 apply steps |
| 100 commits | About 100 apply steps |
| 1000 commits | About 1000 apply steps |
Pattern observation: The time grows linearly with the number of commits to rebase.
Time Complexity: O(n)
This means the time to complete the rebase grows directly with the number of commits being replayed.
[X] Wrong: "Rebasing is instant no matter how many commits there are."
[OK] Correct: Each commit must be applied one after another, so more commits mean more work and more time.
Understanding how git commands scale helps you explain your workflow choices clearly and shows you know what happens behind the scenes.
"What if we changed to an interactive rebase that skips some commits? How would the time complexity change?"
Practice
git rebase command primarily do?Solution
Step 1: Understand the purpose of git rebase
Git rebase moves commits from one base to another to keep history linear and clean.Step 2: Compare with other git commands
Unlike merge, rebase rewrites commit history without creating merge commits.Final Answer:
Moves your commits to a new base commit to create a linear history -> Option DQuick Check:
Rebase = move commits to new base [OK]
- Confusing rebase with merge
- Thinking rebase deletes commits
- Believing rebase creates new branches
main?Solution
Step 1: Recall basic git rebase syntax
The command to rebase current branch onto another isgit rebase <branch>.Step 2: Identify correct option
git rebase main matches the correct syntax:git rebase main.Final Answer:
git rebase main -> Option CQuick Check:
Rebase syntax = git rebase branch [OK]
- Adding unnecessary flags like -m or --merge
- Using --onto without required arguments
- Confusing rebase syntax with merge
git checkout feature
git rebase main
What happens to the commits on
feature branch?Solution
Step 1: Understand what 'git rebase main' does on feature branch
It takes commits from feature and replays them on top of main's latest commit.Step 2: Clarify what happens to commits
Commits are not deleted but moved to appear after main's commits, creating a linear history.Final Answer:
They are replayed on top of the latest commit on main -> Option AQuick Check:
Rebase = replay commits on new base [OK]
- Thinking commits get deleted
- Confusing rebase with merge
- Assuming new branches are created
git rebase main on your feature branch but got conflicts. What is the correct way to continue after resolving conflicts?Solution
Step 1: Identify the correct command after resolving rebase conflicts
After fixing conflicts during rebase, you must rungit rebase --continueto proceed.Step 2: Differentiate from other commands
git merge --continueis for merges,git commit --amendedits commits, andgit rebase --abortcancels rebase.Final Answer:
Run git rebase --continue -> Option AQuick Check:
Fix conflicts then git rebase --continue [OK]
- Using merge commands during rebase
- Aborting rebase instead of continuing
- Trying to amend commits prematurely
Solution
Step 1: Understand effect of rebase on commit history
Rebase rewrites commit history, so remote branch history differs from local.Step 2: Identify correct push method after rebase
You must force push withgit push --forceto update remote branch with rewritten history.Final Answer:
Use git push --force to overwrite remote history -> Option BQuick Check:
Rebase requires force push to update remote [OK]
- Trying normal push after rebase
- Deleting remote branch unnecessarily
- Using push flags unrelated to rebase
