Rebase vs Merge
Rebase vs Merge

Introduction
Rebase and merge are two ways to integrate changes from one branch to another. Understanding when to use each is crucial.
Merge
Creates a merge commit that combines histories:
main: A---B---M
\ /
feature: C---D---E
Pros:
- Preserves complete history
- Shows when branches diverged
- Safe, non-destructive
- Creates merge commits
- History can become complex
- More commits in history
Rebase
Replays commits on top of another branch:
Before rebase:
main: A---B
\
feature: C---D---EAfter rebase:
main: A---B---C'--D'--E'
Pros:
- Linear, clean history
- Easier to follow
- No merge commits
- Rewrites commit history
- Dangerous on shared branches
- Can lose context
When to Use Merge
- Working on public/shared branches
- Want to preserve complete history
- Need to track when branches diverged
- Team workflow requires merge commits
When to Use Rebase
- Local feature branches
- Before submitting pull request
- Want clean, linear history
- Updating feature branch with main changes
Basic Rebase Commands
Rebase Feature onto Main
git checkout feature-branch
git rebase main
Equivalent to:
git rebase main feature-branch
Continue After Resolution
If conflicts occur:
# Fix conflicts
git add .
git rebase --continueSkip current commit
git rebase --skipAbort rebase
git rebase --abort
Interactive Rebase
Squash Commits
git rebase -i HEAD~3
This opens an editor:
pick abc1234 First commit
pick def5678 Second commit
pick ghi9012 Third commit
Change to:
pick abc1234 First commit
squash def5678 Second commit
squash ghi9012 Third commit
This combines commits into one.
Edit Commits
pick abc1234 Add feature
reword def5678 Fix typo
Reorder Commits
Simply reorder lines in the editor.
Rebase vs Merge Examples
Merge Example
git checkout main
git merge feature-login
History becomes:
* Merge branch 'feature-login'
|\
| * Add login feature
* | Fix bug
|/
* Initial commit
Rebase Example
git checkout feature-login
git rebase main
git checkout main
git merge feature-login
History becomes:
* Add login feature
* Fix bug
* Initial commit
Golden Rules
NEVER Rebase Public Branches
Never rebase commits that have been pushed to shared repository.
# BAD - will cause problems
git push --force origin mainGOOD - rebase local only
git rebase main
git push
Use Merge for Team Branches
- Main branch
- Shared feature branches
- Release branches
Use Rebase for Local Branches
- Your personal feature branches
- Before creating PR
- Cleaning up local commits
Common Workflow
Feature Branch Workflow
# Start feature
git checkout -b feature-loginMake commits
git commit -m "Add login form"
git commit -m "Add validation"Update with latest main
git fetch origin
git rebase origin/mainForce push (OK for feature branch not yet merged)
git push --force-with-lease
Pull Request Workflow
# Before creating PR
git checkout feature
git rebase mainPush
git push --force-with-leaseCreate PR
Undoing Rebase
If Something Went Wrong
git reflog
Find the state before rebase:
abc1234 HEAD@{2}: rebase finished: ...
def5678 HEAD@{3}: checkout: moving to main
Recover:
git reset --hard def5678
Or:
git reset --hard ORIG_HEAD
Summary
| Aspect | Merge | Rebase |
|---|---|---|
| History | Preserves branching | Linear |
| Commits | Merge commit created | Commits rewritten |
| Safety | Safe | Risky on shared |
| Use case | Shared branches | Local cleaning |
Next Lesson
Learn about reset, revert, and restore commands.
Quiz - Quiz - Rebase vs Merge
1. What does git rebase do?
2. Why should you never rebase public/shared branches?
3. What is the difference between merge and rebase in terms of history?
4. What does 'git rebase --continue' do?
5. What is interactive rebase used for?