Branching strategies determine how your team collaborates. The wrong strategy creates merge hell; the right strategy enables continuous delivery. Git Flow works for planned releases. GitHub Flow works for continuous deployment. Trunk-based development works for CI/CD at scale. In this lesson, you'll learn each strategy and when to apply them.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand Git Flow branch types (main, develop, feature, release, hotfix)
- Implement GitHub Flow for continuous deployment
- Use trunk-based development for CI/CD
- Choose the right strategy for your team size
- Set up branch protection rules
2. Why Branching Strategies Matter
Real-world scenario: Your team of 10 developers is constantly overwriting each other's changes. Merge conflicts take hours to resolve. Releases are stressful because you don't know what's in production. A branching strategy solves these problems by defining clear rules for when and how to create branches, merge changes, and release code.
3. Core Concepts
Git Flow (Vincent Driessen Model)
Git Flow Branch Structure:
main (production)
↑
├── release/v1.0 (prepare release)
│ ↑
│ └── develop (integration)
│ ↑
│ ├── feature/user-auth
│ ├── feature/payment
│ └── feature/api
│
└── hotfix/critical-bug (emergency fix)
# Git Flow branch types and their purposes
# main (or master): Production-ready code
# - Only code that is or will be in production
# - Every commit is a release
# develop: Integration branch
# - Contains completed features for next release
# - Where feature branches merge
# feature/*: New features
# - Branch from: develop
# - Merge to: develop
# - Naming: feature/user-authentication
git checkout develop
git checkout -b feature/add-docker-support
# ... work on feature ...
git checkout develop
git merge --no-ff feature/add-docker-support
git branch -d feature/add-docker-support
# release/*: Release preparation
# - Branch from: develop
# - Merge to: main AND develop
# - Naming: release/v1.2.0
git checkout develop
git checkout -b release/v1.2.0
# ... bump versions, update docs, bug fixes ...
git checkout main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Release version 1.2.0"
git checkout develop
git merge --no-ff release/v1.2.0
git branch -d release/v1.2.0
# hotfix/*: Emergency production fixes
# - Branch from: main
# - Merge to: main AND develop
# - Naming: hotfix/critical-security
git checkout main
git checkout -b hotfix/security-patch
# ... fix the bug ...
git checkout main
git merge --no-ff hotfix/security-patch
git tag -a v1.2.1 -m "Hotfix: security patch"
git checkout develop
git merge --no-ff hotfix/security-patch
git branch -d hotfix/security-patch
Git Flow Pros and Cons
GitHub Flow (Simpler Alternative)
GitHub Flow Branch Structure:
main (always deployable)
↑
└── feature-branch
↑
└── Pull Request → CI checks → Merge
# Only one long-lived branch: main
# Everything else is a short-lived feature branch
# GitHub Flow workflow
# 1. Create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/new-dashboard
# 2. Make changes, commit often
git add .
git commit -m "Add dashboard components"
git commit -m "Add dashboard API endpoints"
# 3. Push branch and create Pull Request
git push -u origin feature/new-dashboard
# 4. Open Pull Request on GitHub
# - Add description
# - Request reviewers
# - CI runs automatically
# 5. After review and CI passes, merge
git checkout main
git pull origin main
git merge --no-ff feature/new-dashboard
# 6. Delete feature branch
git branch -d feature/new-dashboard
git push origin --delete feature/new-dashboard
# 7. Deploy immediately (or automatically)
Trunk-Based Development (TBD)
Trunk-Based Development:
main (trunk) ← everyone commits directly (or very short branches)
↑
└── release branch (optional, short-lived)
# Features are small (1-2 days max)
# No long-lived branches
# Use feature flags for incomplete features
# Trunk-Based Development workflow
# 1. Developers commit directly to main (small teams)
git checkout main
git pull
git add .
git commit -m "Add small feature"
git push
# 2. Or use very short-lived branches (<1 day)
git checkout -b feature/small-change
# ... work ...
git push -u origin feature/small-change
# Create PR immediately
# Merge within hours, not days
# 3. Use feature flags for incomplete features
# Code:
if feature_flag_enabled("new_feature"):
# new code
else:
# old code
# 4. Release branches (optional, short-lived)
git checkout -b release/2024-01-15 main
# Test, fix, tag
git tag v1.2.3
git push --tags
Comparison Table
┌─────────────────────┬──────────────┬──────────────┬─────────────────────┐
│ Feature │ Git Flow │ GitHub Flow │ Trunk-Based Dev │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Long-lived branches │ 2-3 (main, │ 1 (main) │ 1 (main) │
│ │ develop, │ │ │
│ │ release) │ │ │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Feature branches │ Days-weeks │ Hours-days │ Hours only │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Release frequency │ Weekly/monthly│ Daily │ Multiple times/day │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Team size │ 10-100 │ 1-20 │ 5-1000+ │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Complexity │ High │ Low │ Medium │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Learning curve │ Steep │ Easy │ Moderate │
├─────────────────────┼──────────────┼──────────────┼─────────────────────┤
│ Best for │ Versioned │ Web apps, │ CI/CD, microservices│
│ │ releases │ SaaS │ , large orgs │
└─────────────────────┴──────────────┴──────────────┴─────────────────────┘
Branch Protection Rules
# GitHub branch protection rules (via UI or API)
# Required settings for production branch:
# 1. Require pull request reviews
# 2. Require status checks to pass
# 3. Require up-to-date branches
# 4. Require signed commits
# 5. Include administrators
# 6. Require linear history
# Via GitHub CLI
gh api repos/:owner/:repo/branches/main/protection \
--method PUT \
--field required_status_checks='{"strict":true,"contexts":["continuous-integration"]}' \
--field enforce_admins=true \
--field required_pull_request_reviews='{"required_approving_review_count":2}'
# GitLab branch protection
# Settings → Repository → Protected Branches
# Bitbucket branch permissions
# Repository Settings → Branch permissions
4. Complete Project: Branch Strategy Migration
#!/bin/bash
# migrate_to_gitflow.sh - Script to migrate existing repo to Git Flow
# Install git-flow extension
# Ubuntu: sudo apt install git-flow
# macOS: brew install git-flow
# Initialize git-flow in existing repo
git flow init
# Answer questions:
# Which branch for production releases? [main]
# Which branch for development? [develop]
# Feature branches? [feature/]
# Release branches? [release/]
# Hotfix branches? [hotfix/]
# Support branches? [support/]
# Create develop branch
git checkout -b develop main
# Start a feature
git flow feature start user-authentication
# Work on feature
# ... make changes ...
# Finish feature (merges to develop)
git flow feature finish user-authentication
# Start release
git flow release start v1.0.0
# Update version numbers, last-minute fixes
# ...
# Finish release (merges to main and develop, tags)
git flow release finish v1.0.0
# Start hotfix
git flow hotfix start critical-security-patch
# Fix issue
# ...
# Finish hotfix (merges to main and develop, tags)
git flow hotfix finish critical-security-patch
# Push everything
git push origin main develop --tags
5. Common Errors & Solutions
6. Summary Checklist
7. Next Steps
Next lesson: Git Lesson 3.4: Merge Conflicts & Resolution
You'll learn to:
- Understand what causes merge conflicts
- Resolve conflicts manually
- Use merge tools
- Prevent conflicts with good practices
Comments (0)
This is exactly what I needed! The initContainer approach solved our migration issues completely. Thanks for the detailed guide!
ReplyLeave a Comment