Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 78 ยท Remotes and Collaboration

Branching Strategies: Git Flow, GitHub Flow, Trunk-Based

Compare Git Flow, GitHub Flow and trunk-based development: how each model uses branches, releases and hotfixes, and how to pick the right one for your team.

Git does not tell you how to use branches. It gives you cheap, fast branches and leaves the rules to you. A branching strategy is the team agreement on top: which branches exist, how long they live, where releases come from and how fixes reach production. A good strategy makes releases boring. A bad one produces week-long merges, broken main branches and arguments about what is "ready".

This lesson compares three models you will meet in practice: Git Flow, GitHub Flow and trunk-based development. For each you will see the branch layout, run a small simulation, and learn when it fits. The mechanics of branching and merging are covered in Branches, Merging Branches and Branch Workflows; the team side (reviews, forks) is in Collaboration Workflows.

What a strategy has to decide

Whatever model you choose, it answers the same five questions:

  1. What is the source of truth? Usually one branch (main) that should always be in a known state.
  2. How is work isolated? Per feature, per ticket, per developer, or not at all?
  3. How long may a branch live? Hours, days or months. Longer means more merge pain.
  4. How do releases happen? From main at any time, or from a dedicated release branch?
  5. How do you fix production? A hotfix branch, or an ordinary change that ships immediately?

The answers depend less on taste than on how you ship: a website deployed ten times a day and a desktop app with three supported versions need different rules.

GitHub Flow: one long-lived branch

GitHub Flow is the simplest useful strategy. There is exactly one permanent branch, main, and it is always deployable.

  1. Create a branch from main with a descriptive name.
  2. Commit on it, push it often.
  3. Open a pull request and get a review (Pull Requests).
  4. When tests pass and the review is done, merge into main.
  5. Deploy from main (many teams do it automatically) and delete the branch.
gitGraph
  commit id: "A"
  commit id: "B"
  branch feature-search
  checkout feature-search
  commit id: "search 1"
  commit id: "search 2"
  checkout main
  merge feature-search id: "merge PR #1"
  commit id: "C"

Feature branches are short, main only receives finished work.

The next example plays the whole flow on one machine: a feature branch, a merge commit created with --no-ff so the pull request stays visible in history, and branch cleanup.

Bash GitHub Flow in five commands

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Strengths: very little to learn, fast feedback, works with continuous deployment. Weaknesses: it assumes you can deploy main at any time. It has no answer for "we support version 2.x and 3.x at once", and without good automated tests, a broken merge goes straight to production.

Git Flow: many branches for scheduled releases

Git Flow (Vincent Driessen's 2010 model) adds structure for software that ships in versions. It uses two permanent branches and three kinds of temporary ones:

Branch Lives Branches off Merges into
main forever (receives releases and hotfixes; every commit is a release tag)
develop forever release/*
feature/* days to weeks develop develop
release/* days develop main and develop
hotfix/* hours main main and develop
gitGraph
  commit id: "1.0" tag: "v1.0"
  branch develop
  checkout develop
  commit id: "dev 1"
  branch feature-x
  checkout feature-x
  commit id: "x"
  checkout develop
  merge feature-x
  branch release-1.1
  checkout release-1.1
  commit id: "bump version"
  checkout main
  merge release-1.1 tag: "v1.1"
  checkout develop
  merge release-1.1

Features collect on develop; a release branch stabilizes them; main only ever holds released code.

The flow: features merge into develop. When enough has accumulated, you cut release/1.1 from develop. On that branch you only fix bugs and bump the version. When it is ready, it is merged into main, tagged, and also merged back into develop so the fixes are not lost. A critical bug in production is fixed on a hotfix/1.0.1 branch from main and merged into both main and develop.

Bash A release and a hotfix in Git Flow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Strengths: it matches products with versioned releases, long QA phases or several supported versions. Weaknesses: it is heavy. Features can sit on develop for weeks, merges are big, and the double merge back into develop is easy to forget. Its own author later noted that teams that deploy continuously should prefer something simpler.

Trunk-based development: integrate every day

In trunk-based development everyone integrates into one branch, the trunk (main), at least daily. Branches either do not exist or live for hours: small, reviewed changes land directly or through very short-lived branches. Unfinished work is hidden behind a feature flag (a configuration switch) so incomplete code can ship without being visible to users.

flowchart LR
  A["main"] -->|"small PR, hours"| B["main"]
  B -->|"small PR, hours"| C["main"]
  C -.->|"cut when needed"| R["release/2.3"]
  R -.->|"cherry-pick fixes"| R2["release/2.3 fixes"]

Work flows into one trunk in small steps; a release branch is only cut when a version has to be supported.

Releases are either cut straight from main (continuous delivery) or from a short-lived release/* branch that receives fixes by cherry-pick (Cherry-Pick). The strategy demands strong automation: fast tests, code review that takes minutes, and reliable flags.

Bash Trunk-based: small steps and a feature flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Strengths: the least merge pain, constant integration, fast releases, and a natural fit for continuous delivery. Google and many large companies work this way. Weaknesses: it needs discipline, good tests and a flag system, and it is a poor fit for teams without automation or with long, manual approval gates.

Choosing a strategy

Situation Good fit
Web app or service deployed continuously, strong tests Trunk-based, or GitHub Flow
Small team, simple product, pull-request reviews GitHub Flow
Packaged software with scheduled versions and several supported releases Git Flow, or trunk-based with release branches
Open-source project with outside contributors Fork plus pull requests (Forks and Upstreams) on GitHub Flow
Weak or slow test suite Fix that first; any strategy suffers

Whatever you pick, write it down in CONTRIBUTING.md (branch names, who merges, how to cut a release) and protect main on the server: require pull requests, passing checks and no force-push. A written rule beats a clever diagram.

A few habits help in all models: keep branches short-lived, merge main into your branch (or rebase it) often, name branches by purpose (feature/search, fix/login-timeout), and delete them after merging. The longer a branch lives, the more it costs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which strategy has only one permanent branch that is always deployable?

    GitHub Flow keeps just main. Everything else is a short-lived branch that is merged through a pull request and deleted.
  2. 2. In Git Flow, where does a hotfix branch start and where does it merge?

    The hotfix fixes production, so it starts at main. It is merged back to main (and tagged) and also to develop, so the fix is not lost in the next release.
  3. 3. How does trunk-based development hide unfinished features?

    Small, unfinished changes land on the trunk every day, but a configuration flag keeps the new behavior off until it is ready.
  4. 4. A team ships a mobile app in versions 3.x and 4.x and supports both for a year. Which approach fits best?

    Supporting several versions at once needs a place to ship fixes to each version, which is what release branches provide.

Summary

  • A branching strategy is a team agreement: which branches exist, how long they live, how releases and hotfixes happen.
  • GitHub Flow: one permanent main, short feature branches, pull requests, deploy from main. Simple and good for continuous deployment.
  • Git Flow: main, develop, feature, release and hotfix branches. Suits versioned products with scheduled releases, at the cost of ceremony and bigger merges.
  • Trunk-based development: integrate into main at least daily, hide unfinished work behind feature flags, cut release branches only when needed. It needs strong automation.
  • Choose by how you ship, write the rules down, protect main and keep branches short-lived.

Next: Git Objects