Git Branching Strategies
GitFlow, GitHub Flow o Trunk-Based? Scegli la strategia giusta per il tuo team e progetto.
Git Branching Strategies
🎯 Perché Serve una Strategia?
Senza regole chiare:
- 👥 Il team si pesta i piedi
- 💥 Conflitti infiniti
- 🐛 Bug in produzione
- 😵💫 Nessuno sa cosa c'è in ogni branch
Una buona strategia = sviluppo fluido + release sicure.
🌀 Le 3 Strategie Principali
| Strategia | Complessità | Per Chi |
|---|---|---|
| GitFlow | Alta | Team grandi, release programmate |
| GitHub Flow | Bassa | Team agili, deploy continuo |
| Trunk-Based | Media | Team senior, CI/CD maturo |
1️⃣ GitFlow (Il Classico)
Struttura Branch
Esempio di codice
main ────●─────────────────●─────── (produzione)
↑ ↑
merge merge
↑ ↑
release/1.0 ──●─●────────────●───── (preparazione)
↑ ↑
develop ─●─●─●──●──●──●──●───●─●── (integrazione)
↑ ↑ ↑ ↑
feat feat feat featTipi di Branch
| Branch | Prefisso | Vita | Scopo |
|---|---|---|---|
| main | - | Permanente | Codice in produzione |
| develop | - | Permanente | Integrazione feature |
| feature | feature/ | Temporaneo | Nuove funzionalità |
| release | release/ | Temporaneo | Preparazione release |
| hotfix | hotfix/ | Temporaneo | Fix urgenti produzione |
Quando Usarlo
✅ Release programmate (es: ogni 2 settimane) ✅ QA team separato ✅ Versioni multiple da supportare ❌ Deploy continuo (troppo overhead)
2️⃣ GitHub Flow (Il Semplice)
Struttura
Esempio di codice
main ─●───●───●───●───●─── (sempre deployabile)
↑ ↑ ↑
feat feat fixWorkflow
- Branch da
main - Sviluppa e committa
- Pull Request per review
- Review e approvazione
- Merge in main
- Deploy automatico
Regole Chiave
mainè SEMPRE deployabile- Branch di breve durata (ore/giorni)
- PR obbligatoria per ogni merge
- CI/CD run automatiche
Quando Usarlo
✅ Deploy continuo ✅ Team piccoli/medi ✅ SaaS, web apps ❌ Release programmate ❌ Multiple versioni in produzione
3️⃣ Trunk-Based Development
Filosofia
"Commit direttamente su main, ma con garanzie"
Struttura
Esempio di codice
main ─●─●─●─●─●─●─●─●─ (commit continui)
↓ ↓
deploy deployCome Funziona
- Commit piccoli e frequenti su
main - Feature Flags per nascondere WIP
- CI obbligatorio e veloce (< 10 min)
- Deploy automatico
Feature Flags
javascript
if (featureFlags.newCheckout) {
return <NewCheckout />;
} else {
return <OldCheckout />;
}Quando Usarlo
✅ Team senior con disciplina ✅ CI/CD molto maturo ✅ Cultura di small commits ❌ Team junior ❌ CI lento
📋 Best Practices Universali
Naming Conventions
Esempio di codice
feature/USER-123-add-login
bugfix/ISSUE-456-fix-payment
hotfix/security-patch-xss
release/v2.1.0Commit Messages
Esempio di codice
feat: add user authentication ✅
fix: resolve payment timeout issue ✅
docs: update API documentation ✅
fixed stuff ❌
wip ❌Branch Protection
- 🔒 Require PR reviews
- 🔒 Require status checks
- 🔒 No force push su main
- 🔒 Dismiss stale reviews
🔄 Rebase vs Merge
| Aspetto | Merge | Rebase |
|---|---|---|
| Storia | Mantiene tutti i commit | Storia lineare |
| Conflitti | Una volta | Potenzialmente multipli |
| Sicurezza | Più sicuro | Riscrive storia |
| Quando | Branch condivisi | Branch personali |
Regola d'Oro
Rebase locale, Merge pubblico
bash
# Prima di PR: rebase per storia pulita
git fetch origin
git rebase origin/main
# Merge della PR: merge commit (tracciabilità)
git merge --no-ff feature/xyz⚠️ Errori Comuni
- Branch troppo longevi → Conflitti enormi
- Commit giganti → Review impossibili
- No branch protection → Push accidentali in main
- Strategia non chiara → Caos nel team
- Ignorare i conflitti → Bug nascosti
shell
# ============================================
# GITHUB FLOW - WORKFLOW QUOTIDIANO
# ============================================
# 1. Aggiorna main locale
git checkout main
git pull origin main
# 2. Crea feature branch
git checkout -b feature/USER-123-add-user-settings
# 3. Sviluppa con commit atomici
git add .
git commit -m "feat(settings): add user preferences form"
git add .
git commit -m "feat(settings): implement dark mode toggle"
git add .
git commit -m "test(settings): add unit tests for preferences"
# 4. Tieni aggiornato con main (evita conflitti alla fine)
git fetch origin
git rebase origin/main
# Se conflitti: risolvi, poi `git rebase --continue`
# 5. Push e crea PR
git push -u origin feature/USER-123-add-user-settings
# Poi crea PR su GitHub/GitLab
# 6. Dopo approvazione PR, merge e cleanup
git checkout main
git pull origin main
git branch -d feature/USER-123-add-user-settings
git push origin --delete feature/USER-123-add-user-settings
# ============================================
# GITFLOW - RELEASE CYCLE
# ============================================
# Inizia release
git checkout develop
git pull origin develop
git checkout -b release/v2.1.0
# Bug fix durante release
git commit -m "fix: typo in welcome message"
git commit -m "fix: validation error on mobile"
# Finalizza release
git checkout main
git merge release/v2.1.0
git tag -a v2.1.0 -m "Release 2.1.0"
git push origin main --tags
# Merge back in develop
git checkout develop
git merge release/v2.1.0
git push origin develop
# Cleanup
git branch -d release/v2.1.0
# ============================================
# HOTFIX URGENTE
# ============================================
# 1. Crea hotfix da main
git checkout main
git pull origin main
git checkout -b hotfix/CRITICAL-fix-payment-bug
# 2. Fix e test
git commit -m "fix(payment): resolve timeout on large transactions"
# 3. Merge in main E develop
git checkout main
git merge hotfix/CRITICAL-fix-payment-bug
git tag -a v2.0.1 -m "Hotfix: payment timeout"
git push origin main --tags
git checkout develop
git merge hotfix/CRITICAL-fix-payment-bug
git push origin develop
# 4. Cleanup
git branch -d hotfix/CRITICAL-fix-payment-bug
# ============================================
# COMANDI UTILI
# ============================================
# Vedi grafo branch
git log --oneline --graph --all
# Lista branch locali e remoti
git branch -a
# Pulisci branch locali già mergiati
git branch --merged | grep -v "main\|develop" | xargs git branch -d
# Vedi branch remoti eliminati
git fetch --prune
# Stash temporaneo
git stash
git stash pop
# Cherry-pick commit specifico
git cherry-pick abc123
# Undo ultimo commit (mantieni changes)
git reset --soft HEAD~1
# Alias utili (.gitconfig)
[alias]
co = checkout
br = branch
ci = commit
st = status
lg = log --oneline --graph --all
undo = reset --soft HEAD~1