Rebasing vs Merging
Rebasing and merging are two ways to integrate changes between branches. Understanding when to use each is crucial for maintaining a clean Git history.
The Difference
Merge
Creates a new commit that combines two branches:
Before:
main: A ← B ← C
\
feature: D ← E
After merge:
main: A ← B ← C ← ← ← M
\ /
feature: D ← E ←┘
Rebase
Replays commits on top of another branch:
Before:
main: A ← B ← C
\
feature: D ← E
After rebase:
main: A ← B ← C
\
feature: D' ← E'
The commits D and E are "replayed" as D' and E' on top of C.
When to Use Each
Use Merge When
| Scenario | Why |
|---|---|
| Integrating shared branches | Preserves full history |
| Public/shared branches | Safe, no rewriting |
| Want to see branch history | Merge commit shows integration point |
| Working with others on same branch | Less confusion |
Use Rebase When
| Scenario | Why |
|---|---|
| Cleaning up personal branch | Creates linear history |
| Before merging feature branch | Cleaner PR history |
| Updating from main | Avoids unnecessary merge commits |
| Want linear history | Easier to read |
Basic Rebase
# On feature branch
git checkout feature
git rebase main
This replays feature commits on top of main.
What Happens
- Git finds common ancestor
- Saves your commits temporarily
- Resets to target branch
- Replays your commits one by one
- You now have new commits (new hashes)
Rebase Workflow
Keeping Feature Branch Updated
# On feature branch
git fetch origin
git rebase origin/main
# Push (force because history changed)
git push --force-with-lease
Before Merging a PR
# Ensure your branch is up to date
git checkout feature
git fetch origin
git rebase origin/main
# Resolve any conflicts
# Then push
git push --force-with-lease
# Now the PR is ready for clean merge
Merge Workflow
Merge Main into Feature
git checkout feature
git merge main
# Results in merge commit
Merge Feature into Main
git checkout main
git merge feature
# or with no fast-forward
git merge --no-ff feature
Handling Conflicts
During Rebase
git rebase main
# CONFLICT!
# Fix the conflict in files
git add conflicted-file.js
# Continue rebase
git rebase --continue
# Or abort if it's too messy
git rebase --abort
During Merge
git merge feature
# CONFLICT!
# Fix the conflict
git add conflicted-file.js
# Complete the merge
git commit
The Golden Rule of Rebasing
Never rebase public/shared branches!
# NEVER do this:
git checkout main
git rebase feature # ❌ Rewrites main history!
Only rebase branches that:
- Are local only
- Are your personal feature branch
- Haven't been shared/pushed (or you'll force push to YOUR fork only)
Comparing Histories
Merge History
git log --oneline --graph
* e7f8g9h (HEAD -> main) Merge branch 'feature'
|\
| * d5e6f7g Add feature part 2
| * c4d5e6f Add feature part 1
|/
* b3c4d5e Previous work
* a2b3c4d Initial commit
Rebase History
git log --oneline --graph
* d5e6f7g (HEAD -> main) Add feature part 2
* c4d5e6f Add feature part 1
* b3c4d5e Previous work
* a2b3c4d Initial commit
Linear, no merge commits.
Pull with Rebase
Instead of merge when pulling:
# Default pull (merges)
git pull origin main
# Pull with rebase (cleaner)
git pull --rebase origin main
Configure as default:
git config --global pull.rebase true
Exercise: Rebase Practice
Practice rebasing a feature branch:
Pros and Cons
Merge
Pros:
- Safe for shared branches
- Preserves complete history
- Easy conflict resolution (once)
- Non-destructive
Cons:
- Creates merge commits
- History can be messy
- Harder to read linear history
Rebase
Pros:
- Clean, linear history
- Easier to understand
- No merge commits
- Better for bisecting
Cons:
- Rewrites history (dangerous if shared)
- Can be confusing for beginners
- Must resolve conflicts per commit
- Requires force push
Team Conventions
Agree on a team strategy:
| Strategy | When to use |
|---|---|
| Always merge | Simple, safe, works for everyone |
| Always rebase | Clean history, disciplined team |
| Rebase feature, merge to main | Best of both worlds |
Summary
- Merge combines branches with a merge commit
- Rebase replays commits on top of another branch
- Merge preserves history, rebase creates linear history
- Never rebase public/shared branches
- Use rebase to keep feature branches up to date
- Use merge to integrate features into main
- Choose based on team conventions and situation
In the next lesson, we'll explore interactive rebase for even more control.
التالي · الدرس 30 من 40
Interactive Rebase
قراءة 4 دقيقة
أنهيت 29 من 40 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

