Bird
Raised Fist0
Gitdevops~20 mins

Why rebasing creates linear history in Git - Challenge Your Understanding

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
Challenge - 5 Problems
🎖️
Git Rebase Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does rebasing do to commit history?

Imagine you have a feature branch diverged from main. What does rebasing your feature branch onto main do to the commit history?

AIt creates a new linear sequence of commits by moving feature commits on top of main's latest commit.
BIt merges main into feature, creating a merge commit and preserving all branches.
CIt deletes the feature branch and replaces it with main branch commits.
DIt duplicates all commits from main into feature branch without changing order.
Attempts:
2 left
💡 Hint

Think about how rebasing changes the base of your feature branch commits.

💻 Command Output
intermediate
2:00remaining
Output after rebasing a feature branch

You have a main branch with commits A-B-C and a feature branch with commits D-E diverged from B. You run git rebase main on feature. What does git log --oneline --graph show for feature branch?

A* E<br>* D<br>* B<br>* C<br>* A
B* E' (rebased)<br>* D' (rebased)<br>* C<br>* B<br>* A
C* C<br>* B<br>* A<br>* D<br>* E
D* E<br>* D<br>* C<br>* B<br>* A
Attempts:
2 left
💡 Hint

Rebased commits get new commit IDs and appear on top of main's latest commit.

Troubleshoot
advanced
2:00remaining
Why does rebasing cause conflicts?

When rebasing a feature branch onto main, you encounter conflicts. Why does this happen?

ABecause rebasing resets the repository to an earlier commit, losing recent changes.
BBecause rebasing deletes all commits from main, causing missing files.
CBecause rebasing re-applies commits on top of main, conflicting changes in the same files cause conflicts.
DBecause rebasing merges main and feature branches automatically without checking differences.
Attempts:
2 left
💡 Hint

Think about what happens when commits are replayed on a different base.

🔀 Workflow
advanced
2:00remaining
Choosing between merge and rebase for a clean history

You want to keep your project history linear and easy to read. Which workflow should you use?

AUse <code>git rebase</code> to move feature branch commits on top of main before merging.
BUse <code>git merge --no-ff</code> to always create merge commits.
CUse <code>git cherry-pick</code> to copy commits from main to feature branch.
DUse <code>git reset --hard</code> to discard feature branch commits.
Attempts:
2 left
💡 Hint

Think about which command rewrites history to be linear.

Best Practice
expert
2:00remaining
When should you avoid rebasing a branch?

Which situation is a best practice reason to avoid rebasing a branch?

AWhen you want to update your branch with the latest commits from main.
BWhen you want to squash multiple commits into one for clarity.
CWhen you want to create a linear history before merging your feature branch.
DWhen the branch is shared with others and rebasing would rewrite public history.
Attempts:
2 left
💡 Hint

Consider the impact of rewriting history on collaboration.

Practice

(1/5)
1. What is the main reason rebasing creates a linear history in Git?
easy
A. It creates a new branch automatically
B. It merges all branches into one commit
C. It deletes all previous commits
D. It moves your commits on top of the latest commit from the target branch

Solution

  1. Step 1: Understand what rebasing does

    Rebasing takes your commits and places them on top of another branch's latest commit, replaying them in order.
  2. Step 2: Effect on commit history

    This action removes the branching structure by making your commits appear as if they were made after the latest commit on the target branch, creating a straight line.
  3. Final Answer:

    It moves your commits on top of the latest commit from the target branch -> Option D
  4. Quick Check:

    Rebase = replay commits on top [OK]
Hint: Rebase replays commits on latest branch commit [OK]
Common Mistakes:
  • Thinking rebase merges commits into one
  • Believing rebase deletes old commits
  • Confusing rebase with branch creation
2. Which Git command syntax correctly rebases the current branch onto main?
easy
A. git merge main
B. git rebase main
C. git rebase -m main
D. git checkout main rebase

Solution

  1. Step 1: Identify the correct rebase command

    The correct syntax to rebase the current branch onto main is git rebase main.
  2. Step 2: Check other options for errors

    git merge main merges instead of rebasing; git rebase -m main is invalid syntax; git checkout main rebase is not a valid command.
  3. Final Answer:

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

    Rebase syntax = git rebase branch [OK]
Hint: Use 'git rebase branchname' to rebase [OK]
Common Mistakes:
  • Using merge instead of rebase
  • Adding invalid flags like -m
  • Combining checkout and rebase incorrectly
3. Given this commit history:
main: A --- B --- C
feature: A --- B --- D --- E

After running git rebase main on feature, what will the new commit history look like?
medium
A. main: A --- B --- C
feature: A --- B --- C --- D' --- E'
B. main: A --- B --- C
feature: D --- E
C. main: A --- B --- C --- D --- E
feature: A --- B --- C --- D --- E
D. main: A --- B --- C
feature: A --- B --- D --- E

Solution

  1. Step 1: Understand rebase effect on commits

    Rebasing feature onto main moves commits D and E to be based on C, replaying them as new commits D' and E'.
  2. Step 2: Visualize new commit history

    The main branch remains unchanged. The feature branch now appears as if commits D' and E' were made after C, creating a linear history.
  3. Final Answer:

    main: A --- B --- C
    feature: A --- B --- C --- D' --- E'
    -> Option A
  4. Quick Check:

    Rebase = replay commits on main tip [OK]
Hint: Rebase replays commits after target branch tip [OK]
Common Mistakes:
  • Assuming commits stay unchanged
  • Thinking rebase merges commits
  • Ignoring that rebased commits get new IDs
4. You tried to rebase your branch but got a conflict error. What is the best way to fix this and continue the rebase?
medium
A. Abort the rebase and start over with git rebase --abort
B. Run git merge --continue to resolve conflicts
C. Fix the conflicts manually, then run git rebase --continue
D. Delete the branch and create a new one

Solution

  1. Step 1: Understand rebase conflict handling

    When a conflict occurs during rebase, Git pauses and lets you fix conflicts manually in the files.
  2. Step 2: Continue rebase after fixing conflicts

    After resolving conflicts, you must run git rebase --continue to proceed with the rebase process.
  3. Final Answer:

    Fix the conflicts manually, then run git rebase --continue -> Option C
  4. Quick Check:

    Fix conflicts + git rebase --continue [OK]
Hint: Fix conflicts then run 'git rebase --continue' [OK]
Common Mistakes:
  • Using merge commands during rebase
  • Aborting instead of continuing after fix
  • Deleting branch unnecessarily
5. You have two feature branches, feature1 and feature2, both branched from main. feature1 was rebased onto main and pushed. Now you want to rebase feature2 onto feature1. What is the main benefit of this approach?
hard
A. It creates a linear history combining both features without merge commits
B. It deletes feature1 branch automatically
C. It merges feature2 into main directly
D. It resets feature2 to main ignoring feature1

Solution

  1. Step 1: Understand rebasing one feature branch onto another

    Rebasing feature2 onto feature1 places feature2's commits on top of feature1, making the history linear.
  2. Step 2: Benefit of linear history

    This avoids merge commits and shows a clear sequence of changes, making history easier to read and understand.
  3. Final Answer:

    It creates a linear history combining both features without merge commits -> Option A
  4. Quick Check:

    Rebase feature2 on feature1 = linear combined history [OK]
Hint: Rebase feature2 on feature1 for clean linear history [OK]
Common Mistakes:
  • Thinking rebasing deletes branches
  • Confusing rebase with merge into main
  • Ignoring the order of branches in rebase