Bird
Raised Fist0
Gitdevops~10 mins

git rebase basic usage - Step-by-Step Execution

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
Process Flow - git rebase basic usage
Start on feature branch
Run git rebase main
Git rewinds feature branch commits
Git applies main branch commits
Git reapplies feature branch commits on top
Resolve conflicts if any
Rebase complete, feature branch updated
This flow shows how git rebase moves your feature branch commits on top of the latest main branch commits, updating your branch history.
Execution Sample
Git
git checkout feature
git rebase main
Switch to feature branch and rebase it onto main branch to update feature branch history.
Process Table
StepActionBranch StateResult
1Checkout feature branchfeature points to commit F1On feature branch at F1
2Run git rebase mainfeature at F1, main at M2Start rebase: rewind feature commits
3Rewind feature commitsfeature temporarily at M2Feature commits F1 removed temporarily
4Apply main commitsmain commits M1, M2 appliedBase updated to M2
5Reapply feature commitsfeature commits reapplied on M2Feature branch now at F1' (new commit)
6Check for conflictsNo conflictsRebase completes successfully
7Feature branch updatedfeature points to F1' on top of M2Feature branch history rewritten
💡 Rebase ends when all feature commits are reapplied on top of main branch commits
Status Tracker
VariableStartAfter Step 3After Step 5Final
feature branch commitF1rewound (temporarily removed)F1' (rebased commit)F1'
main branch commitM2unchangedunchangedunchanged
Key Moments - 2 Insights
Why does git rebase 'rewind' feature commits before applying main commits?
Git temporarily removes feature commits to apply main branch commits first, then reapplies feature commits on top, ensuring a linear history as shown in execution_table step 3 and 4.
What happens if there are conflicts during rebase?
Git pauses rebase for you to resolve conflicts manually before continuing, but in this example (step 6) no conflicts occur, so rebase completes automatically.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step are the feature commits temporarily removed?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Check the 'Action' and 'Result' columns in step 3 where feature commits are 'rewound'.
After rebase completes, where does the feature branch point?
AAt original commit F1
BAt rebased commit F1' on top of M2
CAt main branch commit M2
DAt the first commit of main branch
💡 Hint
Look at the 'Branch State' and 'Result' columns in step 7.
If a conflict occurs during rebase, what must you do?
AAbort the rebase immediately
BIgnore conflicts and rebase continues automatically
CResolve conflicts manually and continue rebase
DDelete the feature branch
💡 Hint
Refer to key_moments about conflict handling during rebase.
Concept Snapshot
git rebase basic usage:
- Switch to feature branch: git checkout feature
- Rebase onto main: git rebase main
- Git rewinds feature commits, applies main commits, then reapplies feature commits
- Resolves conflicts if any
- Result: feature branch history updated on top of main branch
Full Transcript
Git rebase moves your feature branch commits on top of the latest main branch commits. First, you switch to your feature branch. Then you run 'git rebase main'. Git temporarily removes your feature commits, applies the latest commits from main, and then reapplies your feature commits on top. If there are conflicts, you resolve them manually. After rebase completes, your feature branch points to new commits that appear as if you started from the latest main branch. This keeps your project history clean and linear.

Practice

(1/5)
1. What does the git rebase command primarily do?
easy
A. Deletes all commits from the current branch
B. Merges two branches together with a merge commit
C. Creates a new branch without any commits
D. Moves your commits to a new base commit to create a linear history

Solution

  1. Step 1: Understand the purpose of git rebase

    Git rebase moves commits from one base to another to keep history linear and clean.
  2. Step 2: Compare with other git commands

    Unlike merge, rebase rewrites commit history without creating merge commits.
  3. Final Answer:

    Moves your commits to a new base commit to create a linear history -> Option D
  4. Quick Check:

    Rebase = move commits to new base [OK]
Hint: Rebase moves commits to a new base, unlike merge [OK]
Common Mistakes:
  • Confusing rebase with merge
  • Thinking rebase deletes commits
  • Believing rebase creates new branches
2. Which of the following is the correct syntax to rebase your current branch onto main?
easy
A. git rebase --merge main
B. git rebase -m main
C. git rebase main
D. git rebase --onto main

Solution

  1. Step 1: Recall basic git rebase syntax

    The command to rebase current branch onto another is git rebase <branch>.
  2. Step 2: Identify correct option

    git rebase main matches the correct syntax: git rebase main.
  3. Final Answer:

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

    Rebase syntax = git rebase branch [OK]
Hint: Use 'git rebase branch-name' to rebase current branch [OK]
Common Mistakes:
  • Adding unnecessary flags like -m or --merge
  • Using --onto without required arguments
  • Confusing rebase syntax with merge
3. Given the following commands run in sequence:
git checkout feature
git rebase main

What happens to the commits on feature branch?
medium
A. They are replayed on top of the latest commit on main
B. They are deleted and replaced by main commits
C. They remain unchanged and main is merged
D. They are copied to a new branch named main

Solution

  1. 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.
  2. Step 2: Clarify what happens to commits

    Commits are not deleted but moved to appear after main's commits, creating a linear history.
  3. Final Answer:

    They are replayed on top of the latest commit on main -> 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 commits get deleted
  • Confusing rebase with merge
  • Assuming new branches are created
4. You ran git rebase main on your feature branch but got conflicts. What is the correct way to continue after resolving conflicts?
medium
A. Run git rebase --continue
B. Run git merge --continue
C. Run git commit --amend
D. Run git rebase --abort to finish

Solution

  1. Step 1: Identify the correct command after resolving rebase conflicts

    After fixing conflicts during rebase, you must run git rebase --continue to proceed.
  2. Step 2: Differentiate from other commands

    git merge --continue is for merges, git commit --amend edits commits, and git rebase --abort cancels rebase.
  3. Final Answer:

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

    Fix conflicts then git rebase --continue [OK]
Hint: After conflicts, use 'git rebase --continue' to proceed [OK]
Common Mistakes:
  • Using merge commands during rebase
  • Aborting rebase instead of continuing
  • Trying to amend commits prematurely
5. You rebased your feature branch onto main and now want to update the remote branch. What must you do to push your changes?
hard
A. Use git push normally without flags
B. Use git push --force to overwrite remote history
C. Delete the remote branch and push again
D. Use git push --no-verify to skip hooks

Solution

  1. Step 1: Understand effect of rebase on commit history

    Rebase rewrites commit history, so remote branch history differs from local.
  2. Step 2: Identify correct push method after rebase

    You must force push with git push --force to update remote branch with rewritten history.
  3. Final Answer:

    Use git push --force to overwrite remote history -> Option B
  4. Quick Check:

    Rebase requires force push to update remote [OK]
Hint: After rebase, always force push to update remote branch [OK]
Common Mistakes:
  • Trying normal push after rebase
  • Deleting remote branch unnecessarily
  • Using push flags unrelated to rebase