Antwa CodeAntwaCode Blog

Programming3 min read

Git Branching Strategy: Branching Models for Teams

Effective Git branching strategies for teams.

Read in Bahasa Indonesia

Git Branching Strategy: Branching Models for Teams

Ever feel confused when collaborating with your team using Git? One person creates a branch, another pushes directly to main, someone forgets to merge, and suddenly the production code is broken before anyone can pull it? 🤯

If so, don't worry — it's a sign that your team needs a clear branching strategy.

In this tutorial, we'll cover popular Git branching models used by development teams. From the most "formal" to the simplest, and when to use each one.


Table of Contents

  1. Why Does Branching Strategy Matter?
  2. Basic Git Branching Concepts
  3. Git Flow
  4. GitHub Flow
  5. GitLab Flow
  6. Trunk-Based Development
  7. Which One Should You Use?
  8. Practical Tips for Teams
  9. Conclusion

Why Does Branching Strategy Matter?

Imagine you have a web project with 5 developers. Everyone is coding on the same branch. Without clear rules, you'll run into:

  • Endless merge conflicts
  • Broken code deployed to production
  • No visibility into which features are ready and which aren't

A branching strategy is like traffic rules for a development team. Without signs, everyone goes wherever they want and ends up crashing into each other.


Basic Git Branching Concepts

Before diving into specific strategies, let's understand the basic terminology:

BranchPurpose
main / masterThe main branch, always production-ready
developIntegration branch for features being worked on
feature/*Branch for working on a new feature
release/*Branch for preparing a new release
hotfix/*Branch for emergency production fixes

Git Flow

Git Flow is a branching model popularized by Vincent Driessen in 2010. It's one of the most structured branching strategies and widely used by large teams.

Branch Structure

main (production)

  ├── hotfix/fix-payment-bug

  └── release/v2.1.0

       └── develop
            ├── feature/user-auth
            ├── feature/payment-system
            └── feature/dashboard

How to Use It

# Initialize Git Flow
git flow init
 
# Start a new feature
git flow feature start user-authentication
# ... work, commit ...
git commit -m "feat: add login form component"
git commit -m "feat: implement JWT token handling"
git flow feature finish user-authentication
 
# Prepare a release
git flow release start v2.1.0
# ... update version, CHANGELOG ...
git commit -m "chore: bump version to 2.1.0"
git flow release finish v2.1.0
 
# Hotfix (emergency fix)
git flow hotfix start fix-payment-crash
# ... fix bug ...
git commit -m "fix: handle null pointer in payment"
git flow hotfix finish fix-payment-crash

Pros & Cons

  • ✅ Very structured and clear
  • ✅ Great for projects with fixed release schedules
  • ❌ Too complex for small projects or startups
  • ❌ Lots of branches to manage

GitHub Flow

GitHub Flow is a simpler branching model that uses only one main branch (main) and feature branches. All changes go into main via pull requests.

Core Principles

  1. The main branch is always deployable
  2. All changes must go through pull requests and code review
  3. After merging, deploy to production immediately

Workflow

main ──●──●──●──●──●──●── (production ready)
        \  /    \  /
         \/      \/
     feature-A  feature-B

How to Use It

# Create a branch from main
git checkout main
git pull origin main
git checkout -b feature/add-search
 
# Work and commit
git add .
git commit -m "feat: add search with debounce"
git push origin feature/add-search
 
# Open a pull request
gh pr create --title "feat: Add search functionality"
 
# Code review → approve → merge → deploy

Pros & Cons

  • ✅ Simple and easy to understand
  • ✅ Great for continuous deployment
  • ❌ Not ideal for long release cycles
  • ❌ Requires high discipline to keep main stable

GitLab Flow

GitLab Flow is a middle ground between the complexity of Git Flow and the simplicity of GitHub Flow. Key features: main branch + environment branches + release branches.

Two Main Patterns

Environment Branching Pattern:

main ──────●─────●─────●─────
             \     \     \
              v     v     v
            staging  production

Release Branching Pattern (multi-version support):

main ──────●─────●─────●─────
             \     \     \
              v     v     v
          17.0  17.1  17.2

How to Use It

# Work on a feature, push to main after review
git checkout -b feature/new-api
git commit -m "feat: add /api/users endpoint"
# ... MR, review, merge to main ...
 
# Promote to staging
git checkout staging
git merge main
git push origin staging
# Auto-deploy to staging
 
# After QA approval, promote to production
git checkout production
git merge staging
git push origin production
# Auto-deploy to production

Pros & Cons

  • ✅ Flexible, can be customized to your team's needs
  • ✅ Clear environment promotion mechanism
  • ❌ Requires deeper understanding of your team's requirements

Trunk-Based Development

Trunk-Based Development is a model where all developers work on one main branch. Feature branches are very short-lived (1-2 days at most) or don't exist at all. This is the model promoted by Google, Facebook, and many large companies.

Core Principles

  1. Everyone works on main
  2. Feature branches must be short (ideally < 1 day)
  3. Use feature flags to control new features
  4. Continuous integration is prioritized

Implementation Example

// Feature flags
const featureFlags = {
  new_checkout: process.env.FF_NEW_CHECKOUT === 'true',
  dark_mode: process.env.FF_DARK_MODE === 'true',
};
 
// In your code
function CheckoutComponent() {
  if (featureFlags.new_checkout) {
    return <NewCheckout />;
  }
  return <LegacyCheckout />;
}
# Daily workflow
git pull origin main
git checkout -b feat/short-lived
# ... work, commit, push (within hours) ...
git push origin feat/short-lived
# Open a small MR, quick review, merge
git branch -d feat/short-lived

Pros & Cons

  • ✅ Very fast for continuous deployment
  • ✅ Reduces merge conflicts because branches are always short
  • ❌ Requires a very strong CI/CD pipeline
  • ❌ Testing must be extremely thorough

Which One Should You Use?

StrategyBest For...
Git FlowLarge teams, scheduled releases (monthly/quarterly), many environments
GitHub FlowStartups, web apps, simple continuous deployment, small-to-medium teams
GitLab FlowGradual environment promotion, multi-version support, GitLab ecosystem
Trunk-BasedMature CI/CD, aggressive continuous deployment, senior developers, ready for feature flags

Tip: Start simple (GitHub Flow) and upgrade to a more complex strategy when needed.


Practical Tips for Teams

1. Document Your Strategy

Create a BRANCHING.md file in the repo root:

# Branching Strategy
- `main` — Production code, always deployable
- `feature/*` — New features, from main
- `bugfix/*` — Bug fixes, from main
 
## Rules
- Never force push to main
- All changes need approval
- CI must pass before merge

2. Branch Naming Convention

feature/user-registration
bugfix/login-error-mobile
hotfix/security-patch-xss
release/v2.1.0

3. Auto-Cleanup Branches

# .github/workflows/cleanup.yml
name: Branch Cleanup
on:
  pull_request:
    types: [closed]
jobs:
  delete-branch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: |
            github.rest.git.deleteRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: `heads/${context.headRef}`
            })

4. Conventional Commits

feat: add search feature
fix: fix login bug on mobile
docs: update API documentation
test: add unit tests for payment

Conclusion

A Git branching strategy isn't about which one is the "coolest" — it's about which one fits best for your team's current situation.

Here's what matters most:

  1. Pick one strategy and have the entire team use it consistently
  2. Document it so everyone understands
  3. Review and adapt every few months
  4. Don't be afraid to change if your current strategy no longer fits

The best branching strategy is the one that makes your team more productive, not the one that makes the workflow more complicated.

Good luck, and may your coding sessions with your team run smoothly! 🚀


Have experience with a particular branching strategy? Share it in the comments below!

More posts