You are developing on a branch. Main has moved forward. How do you integrate?
A---B---C main
/
D---E feature-branch2 options:
Which one to choose? It depends on your goal!
git checkout main
git merge feature-branch A---B---C-------M main
/ /
D---E-----------/ (M = merge commit)git checkout feature-branch
git rebase main A---B---C---D'---E' main| Criterion | MERGE | REBASE |
|---|---|---|
| History | Keeps parallel branches | Linearizes everything |
| Commits | Adds a merge commit | Rewrites the commits |
| Complexity | Simple (1 command) | More complex |
| Conflicts | 1 resolution maximum | Several resolutions possible |
| Safety | Very safe | Dangerous if used badly |
| Traceability | See when branches were created/merged | Loses temporal info |
| Collaboration | Perfect for a team | Careful with shared commits |
| Readability | Complex with many branches | Very clear, linear |
Situations:
Commands:
git checkout main
git pull origin main
git merge feature-branch
git push origin mainSituations:
Commands:
git checkout feature-branch
git rebase main # Resolve conflicts if needed
git checkout main
git merge feature-branch # Fast-forward merge
git push origin main
```
### **GOLDEN RULE:**
**NEVER REBASE SHARED COMMITS!**
```bash
# DANGER - NEVER do this:
git push origin feature-branch # Shared commits
git rebase main # Rewrites shared commits = TEAM CHAOS
# OK - Rebase only locally:
git rebase main # Commits still local = OK
git push origin feature-branch # Push after rebase = OK| Level | Recommended strategy |
|---|---|
| Beginner | Always MERGE |
| Intermediate | MERGE for collaboration, REBASE for local cleanup |
| Expert | The team decides a consistent strategy |
The important thing = consistency in the team, not technical perfection!