What is Rebasing?
Rebasing is the second way of combining work between branches. While merge creates a commit with two parents, rebase essentially takes a set of commits, “copies” them, and places them down somewhere else.The advantage of rebasing is that it creates a nice linear sequence of commits. The commit log/history becomes much cleaner when only rebasing is used.
How Rebase Works
The Rebase Process
When you rebase a branch onto another:- Git finds the common ancestor
- Temporarily saves your commits
- Moves your branch to the target
- Re-applies your commits one by one on top
- Creates new commit copies with different IDs
Sequential vs. Parallel
Rebase makes parallel development appear sequential:- Reality: Features developed in parallel
- After rebase: Looks like one feature was built on top of the other
Visual Example
Before rebase:git rebase main:
C2'is a copy ofC2with a new commit ID- Original
C2still exists somewhere (shown faded in visualization) - History is now linear:
C0 -> C1 -> C3 -> C2'
The Rebase Introduction Level
Level Goal
Create divergent branches and rebase them into a linear history:Expected Result
Rebase Direction
”Rebase ONTO”
The commandgit rebase <target> means:
“Take my current branch and rebase it ONTO the target”
Think of rebase as moving your branch to have a new “base” - hence “re-base”.
Common Pattern
Typical workflow after rebasing:- Rebase your feature onto main (linearize)
- Switch to main
- Rebase main onto feature (fast-forward)
Commit Copies
Why Copies?
Rebasing creates new commits because:- Commits are immutable (can’t change their parents)
- New commits need different parent pointers
- Different parent = different commit ID
Identifying Copies
In Learn Git Branching:- Original:
C2 - Copy:
C2'(prime mark) - Original shown faded/translucent
- Copy shown solid at new location
Rebase vs. Merge
Visual Comparison
Merge Result:Key Differences
Merge
- Preserves true history
- Non-destructive
- Creates merge commits
- Shows parallel development
- History can be complex
Rebase
- Creates linear history
- Rewrites history
- No merge commits
- Appears sequential
- Cleaner log
When to Use Rebase
Good Use Cases
Feature Branches
Rebase onto main before merging to keep history linear.
Personal Branches
Clean up your work before sharing with others.
Local Changes
Rebase local commits that haven’t been pushed.
Clean History
Make commit history easier to follow and understand.
When NOT to Rebase
Advanced Rebasing
Interactive Rebase
While not in Learn Git Branching’s basic levels, interactive rebase allows:- Reordering commits
- Combining commits (squash)
- Editing commit messages
- Dropping commits
Rebase with Specific Range
Rebase specific commits:The Golden Rule of Rebasing
Why?- Other developers may have based work on your commits
- Rewriting shared history causes conflicts
- Forces others to re-sync their work
Rebase Implementation
From the source code:Practical Examples
Example 1: Update Feature Branch
Example 2: Clean Feature Before Merge
Rebase Mechanics
Fast-Forward After Rebase
After rebasing a branch onto main, main can fast-forward: After rebase:git checkout main; git rebase feature:
Ancestor Relationships
After rebase, the rebased branch becomes a descendant:- Before:
bugFixandmainare siblings (both fromC1) - After:
bugFixis a child ofmain(based onC3)
Handling Conflicts During Rebase
While Learn Git Branching doesn’t show conflicts, in real Git:If Conflicts Occur
Best Practices
Rebase Often
Regularly rebase feature branches onto main to avoid big conflicts.
Before Pushing
Rebase and clean up commits before pushing to shared repository.
Communicate
Let team know if rebasing shared branches (though generally avoid this).
Keep It Local
Only rebase commits that exist in your local repository.
Understanding the Visualization
Original Commits
After rebase, original commits appear:- Faded/translucent
- Still in their original position
- Eventually garbage collected (in real Git)
Copied Commits
- Solid/opaque
- In new position (on top of target)
- Active and referenced by branch
Why Show Both?
Learn Git Branching shows both to help you understand:- What changed (location and parent)
- That original commits still exist
- Why commit IDs changed
Common Patterns
Pattern 1: Feature Branch Workflow
Pattern 2: Keep Branch Current
Rebase Interactive Options
While not in basic levels, interactive rebase supports:pick- Use the commitreword- Change commit messageedit- Modify commitsquash- Combine with previousdrop- Remove commit
Related Concepts
- Merging - Alternative integration strategy
- Branching - Creating branches to rebase
- Commits - Understanding what gets copied
Next Steps
After mastering rebase:- Learn when to use rebase vs. merge
- Practice interactive rebase for commit cleanup
- Understand remote branch implications
- Explore advanced rebase scenarios