Software engineering / Git branching strategies

Git branching strategies

Two ways for a team to share one codebase: Git Flow with its five kinds of branch, and trunk-based development with one line and feature flags. We draw both and see what each suits.

Beginner7 steps~7 min
Commit — one saved change
Branch — a side line of commits, merged back later
Tag — marks a commit as a released version
Feature flag — a switch that hides unfinished code
// THE SHORT VERSION

In 30 seconds

A branching strategy is a team’s agreement on how to use Git branches: where new work starts, where it is merged, and where releases come from.

Git Flow keeps two permanent branches, one for released code and one for the next version, plus temporary branches for features, releases and urgent fixes. It suits software shipped as numbered versions on a schedule.

Trunk-based development keeps one branch that everyone merges into at least daily, hiding unfinished work behind feature flags. It suits services that are updated continuously.

Git branching strategies, step by step

STEPS01 / 07

Simplified: these are tendencies, not rules, and many teams use something in between, such as GitHub Flow (short feature branches merged straight into main). Git Flow originally called its main branch master.

STEP 01 / 07 Why branches need rules
THE MATH
branch: work apart merge: bring it back longer apart = harder merge
  1. Step 1: Why branches need rules. Git records a project as a line of saved changes called commits. A branch lets you work on a copy of that line without disturbing anyone, and a merge brings your commits back. Meanwhile the others keep changing main, so the longer a branch lives, the harder the merge. A branching strategy is a team’s agreement on handling this.
  2. Step 2: Git Flow: two permanent lines. Git Flow keeps two branches forever. Main holds only what has been released, and each release is marked with a version tag such as v1.0. Develop is where the next version takes shape. Every task gets its own feature branch, which starts from develop and merges back into develop when the task is done.
  3. Step 3: Git Flow: the release branch. When develop holds enough for a new version, a release branch is cut from it. Only bug fixes go in while it is tested, so new feature work can carry on in develop without disturbing it. When it is ready, the release branch merges into main, which is tagged v1.1, and also back into develop so the fixes are kept.
  4. Step 4: Git Flow: the hotfix. A serious bug turns up in v1.1 and cannot wait for the next planned release. A hotfix branch starts from main, the exact code that customers have, and carries only the repair. It merges into main as v1.1.1 and into develop, so the bug does not return in v1.2. That makes five kinds of branch to keep track of.
  5. Step 5: Trunk-based: one line. Trunk-based development keeps a single permanent branch, main, also called the trunk. Branches are small and short: a few hours of work, a day or two at most, then merged. Automated tests run on every merge to keep main working. Because nobody is ever far from main, merges stay small and conflicts are rare.
  6. Step 6: Feature flags. A big feature does not fit in one day. In trunk-based development it is merged anyway, in parts, behind a feature flag: a switch in the code that keeps the new path turned off. The unfinished work reaches main and even production, but users still get the old behaviour. When the last part is in, the flag is turned on.
  7. Step 7: Which one fits?. The choice follows how you release more than how many you are. Git Flow suits software that ships as numbered versions on a schedule, where old versions must still be patched. Trunk-based suits a web service with one live version, updated many times a day; it works for two people or thousands, but only with fast, trusted tests.
VALUES SO FAR
branchwork apartidea
main····Git Flow
develop····Git Flow
release····tagged
hotfix····tagged
trunk····branch
lifetime····branch
flag····release
choose····fit
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?