Skip to main content

Managing Git Branches


Table of contents


  1. Branch concepts
  2. Creating and deleting branches
  3. Switching between branches
  4. Merging branches
  5. Branching strategies
  6. 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?

UseExample
New featurefeature/login
Bug fixfix/button-click
Experimentationexperiment/new-ui
Releasesrelease/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 ──────────────────●─────────┘
BranchUse
mainProduction
developIntegration
feature/*New features
release/*Release preparation
hotfix/*Urgent fixes

GitHub Flow

Simpler, ideal for continuous deployment:

main ────●────●────●────●────●────
↑ ↑ ↑
feature ─● │ │
│ │
feature ──────● │

feature ───────────●
  1. Create a branch from main
  2. Make commits
  3. Open a Pull Request
  4. Review and discussion
  5. Merge into main
  6. 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

PracticeReason
Short-lived branchesFewer conflicts
One goal per branchEasier reviews
Merge main regularlyStay up to date
Delete after mergeKeep things clean

❌ Avoid

PracticeRisk
Long-lived branchesDifficult conflicts
Vague names (fix, test)Confusion
Working directly on mainUnstable code

Command summary

CommandDescription
git branchList the branches
git branch nomCreate a branch
git checkout -b nomCreate and switch to the branch
git switch nomSwitch branch
git merge nomMerge a branch
git branch -d nomDelete a branch

🔝 Back to table of contents