Case Study: Merge and Rebase
Table of contents
- Introduction
- Base scenario
- Option 1: Git Merge
- Option 2: Git Rebase
- Visual comparison
- When to use which?
- Practical exercise
1 - Introduction
When several people work on a project, branches diverge. To integrate changes, two main options exist:
- Merge: Merges the branches by creating a merge commit
- Rebase: Rewrites the history to linearize the commits
💡 This is one of the most debated topics in the Git community!
🔝 Back to table of contents
2 - Base scenario
Initial situation
You are working on a feature branch feature/login while main has evolved:
A---B---C feature/login
/
D---E---F---G main
- D, E: Initial commits on main
- A, B, C: Your commits on feature/login
- F, G: New commits on main (made by others)
The problem
Your feature/login branch does not have the latest changes from main. You need to integrate those changes.
🔝 Back to table of contents
3 - Option 1: Git Merge
How it works
The merge creates a merge commit that combines the two branches:
git checkout feature/login
git merge main
Result
A---B---C---M feature/login
/ /
D---E---F---G--- main
M is the merge commit that has two parents: C and G.
Advantages of Merge
| Advantage | Description |
|---|---|
| ✅ History preserved | You see exactly when the branches were merged |
| ✅ Safety | No history rewriting |
| ✅ Traceability | Easy to understand the workflow |
| ✅ Reversible | You can undo a merge |
Drawbacks of Merge
| Drawback | Description |
|---|---|
| ❌ Cluttered history | Many merge commits |
| ❌ Complex graph | Harder to read with git log --graph |
Merge commands
# Se placer sur la branche cible
git checkout feature/login
# Merger main dans feature/login
git merge main
# En cas de conflits, résoudre puis :
git add .
git commit
🔝 Back to table of contents
4 - Option 2: Git Rebase
How it works
The rebase moves your commits to the tip of main:
git checkout feature/login
git rebase main
Result
A'--B'--C' feature/login
/
D---E---F---G main
Commits A, B, C are recreated as A', B', C' with new hashes.
Advantages of Rebase
| Advantage | Description |
|---|---|
| ✅ Linear history | Clean, easy-to-read graph |
| ✅ No merge commits | Tidy history |
| ✅ Easy to follow | The order of commits is logical |
Drawbacks of Rebase
| Drawback | Description |
|---|---|
| ❌ Rewrites history | Dangerous if already shared |
| ❌ Multiple conflicts | May require resolving conflicts several times |
| ❌ Modified hashes | Commits have new identifiers |
Rebase commands
# Se placer sur la branche à rebaser
git checkout feature/login
# Rebaser sur main
git rebase main
# En cas de conflits, résoudre puis :
git add .
git rebase --continue
# Pour annuler le rebase
git rebase --abort
🔝 Back to table of contents
5 - Visual comparison
Before (initial situation)
A---B---C feature/login
/
D---E---F---G main
After Merge
A---B---C---M feature/login
/ /
D---E---F---G--- main
After Rebase
A'--B'--C' feature/login
/
D---E---F---G main
Comparison table
| Aspect | Merge | Rebase |
|---|---|---|
| History | Non-linear | Linear |
| Extra commits | Yes (merge commit) | No |
| Hashes preserved | ✅ Yes | ❌ No |
| Safety | ✅ High | ⚠️ Risky if shared |
| Traceability | ✅ Complete | Partial |
| Readability | Medium | ✅ Excellent |
🔝 Back to table of contents
6 - When to use which?
Use MERGE when:
- ✅ The branch has been shared (pushed to GitHub)
- ✅ You want to preserve the exact history
- ✅ You are working as a team on the same branch
- ✅ You are integrating a finished feature into main
- ✅ You are not sure
Use REBASE when:
- ✅ The branch is local (never shared)
- ✅ You want a clean history before a merge
- ✅ You are updating your branch with main
- ✅ You are cleaning up your commits before a PR
Golden rule
⚠️ NEVER rebase a public branch!
If other people are working on it, use merge.
Recommended workflow
# 1. Mettre à jour main
git checkout main
git pull
# 2. Rebaser votre feature branch (si locale)
git checkout feature/login
git rebase main
# 3. Une fois terminé, merger dans main
git checkout main
git merge feature/login # Fast-forward possible !
# 4. Pousser
git push origin main
🔝 Back to table of contents
7 - Practical exercise
Setup
# Créer un dépôt de test
mkdir test-merge-rebase
cd test-merge-rebase
git init
# Créer des commits sur main
echo "Initial" > file.txt
git add file.txt
git commit -m "Initial commit"
echo "Line 2" >> file.txt
git commit -am "Add line 2"
# Créer une branche feature
git checkout -b feature/test
echo "Feature line" >> file.txt
git commit -am "Add feature line"
# Retourner sur main et ajouter un commit
git checkout main
echo "Main line" >> file.txt
git commit -am "Add main line"
Test the Merge
git checkout feature/test
git merge main
git log --oneline --graph
Test the Rebase (redo the setup first)
git checkout feature/test
git rebase main
git log --oneline --graph
Observe the difference
Compare the two histories with git log --oneline --graph --all.