Git Course 0%

Branching strategies

Intermediate Core Git ≈ 10 min

What you will learn

  • The four common strategies and what distinguishes them
  • Which problems each one solves, and what each costs
  • How to find out your team's strategy without asking

After this lesson you can

  • I can read a repository's branch list and tell how the team works

Why this matters

A branching strategy is a team's answer to "where does work start and where does it end". You rarely choose one, but you always have to work inside one, and recognising which you are in tells you where to branch from and what to expect at merge time.

The four

Four branching strategies compared Four cards. GitHub Flow: one long-lived branch, main, with short feature branches merged back and deployed from main; simple, and the most common for web projects. GitLab Flow: GitHub Flow plus environment branches such as staging and production, or release branches, so a deploy is a merge into an environment branch. Git Flow: long-lived main and develop branches plus feature, release and hotfix branches; heavy, and suited to versioned software with several supported releases. Trunk-based: everyone commits to main many times a day behind feature flags, with branches living hours rather than days. Each card lists what it fits. GitHub Flow one long-lived branch: main short feature branches merged back Fits: web projects, continuous deployment, most teams GitLab Flow main plus environment branches deploying is merging into one of them main prod Fits: staged deployments, regulated releases Git Flow main and develop, plus release and hotfix branches; the most machinery of the four main dev Fits: versioned products with supported releases Trunk-based everyone commits to main, many times a day branches live hours; flags hide unfinished work Fits: experienced teams with strong automated tests To find out which one your team uses, read CONTRIBUTING.md, look at the branch list, and look at what merges into what.
GitHub Flow, GitLab Flow, Git Flow, trunk-based: from simplest to most machinery, and what each fits.

GitHub Flow

One long-lived branch, main, which is always deployable. Work happens on short branches that merge back through a pull request and deploy from main.

Rules: main is always releasable; branch for anything; merge through review; deploy after merging.

Suits: web applications, teams that deploy often, most small and medium teams. It is the default assumption of both platforms' interfaces.

Costs: it assumes you can always ship what is on main, which needs decent automated tests and the ability to deploy easily.

GitLab Flow

GitHub Flow plus environment branches. main is still the line of development, and deploying means merging main into staging, then production; or, for versioned software, cutting release/* branches from main.

Rules: work merges into main; environments are branches downstream of main; nothing is committed to an environment branch directly.

Suits: teams with a staging environment, regulated release processes, or software that ships versions rather than deploying continuously.

Costs: more branches to keep track of, and a real risk of environment branches drifting if a fix is applied to one directly.

Git Flow

Two long-lived branches, main (what is released) and develop (what is next), plus feature/*, release/* and hotfix/* branches with defined merge routes between them.

Rules: features branch from develop; releases branch from develop and merge to both main and develop; hotfixes branch from main and merge to both.

Suits: installed software with several supported versions, long release cycles, formal QA phases.

Costs: the most machinery of the four. Its own author has since described it as unsuited to continuously delivered web applications, which is worth knowing when someone proposes it by default.

Trunk-based development

Everyone commits to main frequently, often several times a day. Branches exist but live hours rather than days, and unfinished work is hidden behind feature flags rather than kept on a branch.

Rules: integrate at least daily; keep changes small; rely on automated tests; use flags for anything incomplete.

Suits: experienced teams with strong test suites and a culture of small changes. It produces the fewest conflicts of any strategy, because nothing diverges for long.

Costs: it needs the tests and the flags. Without them it is just "everyone commits to main", which is how main gets broken twice a day.

What actually distinguishes them

Two questions separate all four:

Question GitHub Flow GitLab Flow Git Flow Trunk-based
How many long-lived branches? One One plus environments Two plus One
How long does a branch live? Days Days Days to weeks Hours

Everything else is detail. If you know the answers for your team, you know how to work there.

Finding out which one your team uses

Nobody will announce it. Four ways to read it off the repository:

  1. CONTRIBUTING.md. If it exists, it usually says, and it is the first file to read in any repository (lesson 13.8).
  2. The branch list. A develop branch means Git Flow. staging and production branches mean GitLab Flow. Only main plus short-lived branches means GitHub Flow or trunk-based.
  3. The merge history. git log --graph --oneline -30 shows what merges into what. Frequent tiny merges into main suggest trunk-based; a release/* branch merging into main suggests Git Flow.
  4. The protected branches. Whatever is protected is what the team considers precious, which is usually the answer.
Terminal
$ git branch -r
  origin/HEAD -> origin/main
  origin/docs/12-windows-install-steps
  origin/main
  origin/release/1.2

That listing says: main plus a release branch, so releases are cut and supported, and work is short-lived. GitLab Flow's release variant, in other words.

How to do it

Two commands read a team's strategy in under a minute:

Terminal
$ git branch -r
$ git log --graph --oneline -30

The first shows the long-lived branches; the second shows how work gets into them.

Common mistakes

  • Assuming GitHub Flow everywhere. A develop branch changes where your branch starts.
  • Branching from main in a Git Flow repository, where features come from develop.
  • Proposing a strategy change in your first month. Learn why the current one exists first; there is usually a reason, and sometimes it is a good one.
  • Committing directly to an environment branch, which makes it drift from main.
  • Treating "trunk-based" as permission to skip review. It means small changes integrated often, not unreviewed ones.

Try it yourself

Goal: identify the strategy of three real repositories.

  1. Pick three public repositories you use, or three of your team's.
  2. For each, run git branch -r on a clone, or read the branch list on the platform.
  3. Look for develop, staging, production and release/*.
  4. Read CONTRIBUTING.md if there is one, and see whether it agrees with your guess.
  5. Write one sentence per repository saying which strategy it uses and where you would branch from.

Expected result: three sentences, each of which tells you exactly what to do on your first change in that repository.

Show solution

Step 4 is the check. When the documented process and the branch list disagree, the branch list is usually right and the document is out of date, which is itself worth knowing and worth a small merge request fixing the document.

Check yourself

1. A repository has main and develop as long-lived branches. Which strategy, and where do you branch from?
2. What defines trunk-based development?
3. What is the fastest way to find out how a team works?

Key terms

Branch main Release Protected branch