Managing Git Branches
Table of contents
- Branch concepts
- Creating and deleting branches
- Switching between branches
- Merging branches
- Branching strategies
- Best practices
1 - Branch concepts
What is a branch?
A branch is simply a pointer to a commit.
A---B---C feature
/
D---E---F---G main
↑
HEAD
Why use branches?
| Use | Example |
|---|---|
| New feature | feature/login |
| Bug fix | fix/button-click |
| Experimentation | experiment/new-ui |
| Releases | release/v2.0 |
The main/master branch
- The project's main branch
- Contains the production code
- Should always be stable
🔝 Back to table of contents
2 - Creating and deleting branches
Create a branch
# Créer une branche (sans y aller)
git branch feature/login
# Créer et aller sur la branche
git checkout -b feature/login
# Syntaxe moderne
git switch -c feature/login
Create from a specific commit
git branch feature/old-version abc1234
List branches
# Branches locales
git branch
# Branches avec dernier commit
git branch -v
# Toutes les branches (locales + remote)
git branch -a
# Branches mergées
git branch --merged
# Branches non mergées
git branch --no-merged
Delete a branch
# Supprimer (si mergée)
git branch -d feature/login
# Forcer la suppression
git branch -D feature/login
# Supprimer sur le remote
git push origin --delete feature/login
Rename a branch
# Renommer la branche actuelle
git branch -m nouveau-nom
# Renommer une autre branche
git branch -m ancien-nom nouveau-nom
🔝 Back to table of contents
3 - Switching between branches
Switch branch
# Ancienne syntaxe
git checkout feature/login
# Nouvelle syntaxe (Git 2.23+)
git switch feature/login
Return to the previous branch
git checkout -
git switch -
Check the current branch
git branch --show-current
git status # Affiche aussi la branche
Watch out for uncommitted changes
# Si vous avez des modifications non commitées
git checkout feature/login
# error: Your local changes would be overwritten
# Solutions :
# 1. Commiter
git commit -am "wip"
# 2. Stasher
git stash
git checkout feature/login
git stash pop # Quand vous revenez
🔝 Back to table of contents
4 - Merging branches
Basic merge
# Aller sur la branche destination
git checkout main
# Fusionner la feature branch
git merge feature/login
Types of merge
Fast-forward merge
When main hasn't changed since the branch was created:
Avant:
D---E main
\
A---B feature
Après (fast-forward):
D---E---A---B main, feature
git merge feature # Fast-forward automatique
Merge commit
When main has evolved:
Avant:
A---B feature
/
D---E---F main
Après:
A---B
/ \
D---E---F---M main
git merge feature # Crée un merge commit
Force a merge commit
git merge --no-ff feature/login
Advantage: The history clearly shows where the feature was integrated.
Undo a merge
# Pendant le merge (conflits)
git merge --abort
# Après le merge
git reset --hard HEAD~1
🔝 Back to table of contents
5 - Branching strategies
Git Flow
A complete structure for large projects:
main ─────────────●─────────────────●───────
↑ ↑
release ─────────●───────● ●───────
↑ ↑ ↑
develop ────●────●───────●─────────●───────
↑ ↑ ↑ ↑
feature ────●────● │ │
│ │
hotfix ──────────────────●─────────┘
| Branch | Use |
|---|---|
main | Production |
develop | Integration |
feature/* | New features |
release/* | Release preparation |
hotfix/* | Urgent fixes |
GitHub Flow
Simpler, ideal for continuous deployment:
main ────●────●────●────●────●────
↑ ↑ ↑
feature ─● │ │
│ │
feature ──────● │
│
feature ───────────●
- Create a branch from
main - Make commits
- Open a Pull Request
- Review and discussion
- Merge into
main - Deploy
Trunk-Based Development
Everyone works on main with feature flags:
main ────●────●────●────●────●────
↑ ↑ ↑ ↑ ↑
dev1 dev2 dev1 dev3 dev2
Ideal for: Experienced teams, very frequent deployment.
🔝 Back to table of contents
6 - Best practices
Branch naming
# Format recommandé
type/description-courte
# Exemples
feature/user-authentication
fix/login-button
hotfix/security-patch
release/v2.1.0
✅ Do
| Practice | Reason |
|---|---|
| Short-lived branches | Fewer conflicts |
| One goal per branch | Easier reviews |
| Merge main regularly | Stay up to date |
| Delete after merge | Keep things clean |
❌ Avoid
| Practice | Risk |
|---|---|
| Long-lived branches | Difficult conflicts |
Vague names (fix, test) | Confusion |
| Working directly on main | Unstable code |
Command summary
| Command | Description |
|---|---|
git branch | List the branches |
git branch nom | Create a branch |
git checkout -b nom | Create and switch to the branch |
git switch nom | Switch branch |
git merge nom | Merge a branch |
git branch -d nom | Delete a branch |