Skip to content
$ lynuxpro
← Git

LESSON 16 OF 21 · ADVANCED · 18 MIN

Branching strategies

HOW IT WORKSTrunk-based development

mainalways releasableshort branch< 1 dayshort branch< 1 dayMerge requestreview + CIFeature flagshide unfinished workDeploymany times a day
1/5

Developers work on small branches that live for hours, not weeks.

Git lets you branch any way you like, which is exactly why teams need an agreed branching strategy: rules for which branches exist, how long they live, how changes get into main, and how releases are made. The right strategy depends on how often you release and how many people work in the repository. This lesson compares the four strategies you will meet in practice, and explains why most DevOps teams today use trunk-based development.

In simple words

  • Branching strategy: a team's agreed rules for creating, naming and merging branches.
  • Trunk-based development: everyone merges small changes into main often, at least daily. The usual choice for CI/CD.
  • GitHub Flow / GitLab Flow: short feature branches merged into main through reviews. GitLab Flow adds environment or release branches.
  • GitFlow: an older model with develop, feature, release and hotfix branches. Suits slow, versioned releases.

What a strategy decides

  • Which long-lived branches exist (main, develop, release/*)?
  • How long feature branches live: hours, days or weeks?
  • How changes are reviewed and merged (merge requests, required approvals, pipelines)?
  • How releases and hotfixes are made?
  • What is deployed, and when?

1. Trunk-based development

Everyone integrates into one branch, main (the "trunk"), at least daily.

main ──●──●──●──●──●──●──●──●──►   always releasable, deployed continuously        \   /   \  /  \   /         ●─●     ●●    ●─●          short-lived branches: hours to a day or two
OUTPUTterminalutf-8 · 3L
  • Feature branches are small and short-lived, merged through merge requests with review and a passing pipeline.
  • main is always in a releasable state; every merge can be deployed.
  • Unfinished features are merged hidden behind feature flags instead of waiting on a long branch.
  • Releases are tags on main (or short release branches cut from it when needed).

Pros: small changes, few and tiny conflicts, fast feedback, works naturally with CI/CD and continuous deployment. This is what the DORA research associates with high-performing teams. Cons: needs good automated tests, a fast pipeline and feature-flag discipline.

2. GitHub Flow

A simple, popular variant of trunk-based development:

  1. Branch from main with a descriptive name.
  2. Commit and push; open a merge (pull) request early.
  3. Review, discuss, and let CI run.
  4. Merge into main.
  5. Deploy main (often automatically).

It is ideal for web services and SaaS products deployed many times a day, and for most internal tooling and infrastructure repositories.

3. GitLab Flow

GitHub Flow plus environment branches or release branches, for teams that cannot deploy every merge immediately:

main ──●──●──●──●──●──●──►            \       \staging ─────●───────●──►       merged forward after checks                      \production ────────────●──►     merged forward when approved
OUTPUTterminalutf-8 · 5L

Changes always flow downstream (main → staging → production), never the other way, so every environment branch is a subset of main. A variant uses release/1.4-style branches for versioned products, with fixes made on main first and cherry-picked to supported release branches.

Many modern teams replace environment branches with one branch plus promotion of build artifacts (the same image tag moves from staging to production via the pipeline or GitOps), which avoids merging between branches entirely.

4. GitFlow

Designed in 2010 for software with scheduled, versioned releases:

main      ──●───────────────●──────────●──►   only released versions, tagged             \             /          /release/1.2   \       ●───●     hotfix/1.2.1               \     /          /develop ────●───●───●──●──●────●──►           integration branch             \     /    \   /feature/x     ●───●      ●─●                  can live for weeks
OUTPUTterminalutf-8 · 7L
  • develop collects finished features; main contains only released code.
  • release/* branches stabilise a version; hotfix/* branches fix production and merge into both main and develop.

Pros: clear model for products with versions and parallel maintenance (installed software, mobile apps, firmware). Cons: many long-lived branches, big merges, slow feedback, and constant merging between main and develop. Its author has since recommended simpler flows for continuously delivered software.

Comparing them

Trunk-based / GitHub FlowGitLab FlowGitFlow
Long-lived branchesmainmain + env or releasemain, develop, release, hotfix
Feature branch lifehours–2 daysdaysdays–weeks
Release stylecontinuousstaged / versionedscheduled versions
Merge painlowmediumhigh
FitsSaaS, services, IaCregulated or staged deploysversioned products

The practices that make any strategy work

  • Protected branches. No direct pushes or force pushes to main; changes arrive through merge requests.
  • Required checks. The pipeline (build, tests, linting, security scans) must pass before merging.
  • Code review. At least one approval; code owners (CODEOWNERS file) for critical paths.
  • Small merge requests. Aim for changes reviewable in under 20 minutes.
  • Up-to-date branches. Rebase or merge main into your branch often; GitLab can require the branch to be up to date before merging.
  • Clear naming. feature/, fix/, hotfix/, ticket numbers.
  • Delete merged branches. GitLab can do it automatically when a merge request merges.
  • Feature flags to separate deploying code from releasing a feature.

Practise the trunk-based loop

git config --global user.name "Leela Kumar"git clone https://gitlab.com/acme/shop.gitcd shopgit switch -c feature/SHOP-142-health-uptimeprintf 'from flask import Flask\n\nfrom pricing import total\n\napp = Flask(__name__)\nSHOW_UPTIME = False  # feature flag\n' > app.pygit commit -am "Add uptime flag (off by default)"git push -u origin feature/SHOP-142-health-uptimegit switch maingit pullgit branch --merged
SHELLbashutf-8 · 10L

In a real project, you would now open the merge request from the link GitLab printed, let the pipeline run, get an approval and merge. The feature flag lets the change merge today, switched off, while the rest of the feature follows in further small merge requests.

Real-world scenario: from GitFlow to trunk-based

A team running GitFlow released every three weeks. Release branches took days to stabilise because develop contained three weeks of features merged together, and hotfixes regularly went missing from develop. They moved to trunk-based development in three steps: feature flags for unfinished work, a pipeline that ran in under ten minutes, and a rule that branches older than two days are discussed at stand-up. Six months later they deployed several times a day, and "merge day" no longer existed.

Common mistakes

  • Copying GitFlow by default for a web service that could deploy continuously.
  • Long-lived feature branches in any strategy; integration pain grows with branch age.
  • Environment branches that drift, with fixes applied to production but not main.
  • No branch protection, so main gets broken by direct pushes.
  • Huge merge requests that nobody can review properly.

Practice

  1. Describe which strategy fits: a mobile app with monthly App Store releases; a payment API deployed daily; a Terraform repository for cloud infrastructure.
  2. Write the branch protection rules you would set for main in your team.
  3. Run the trunk-based loop above in the practice terminal with your own branch name.
  4. Think of a feature you would hide behind a flag and describe how you would roll it out.

Cheat sheet

Trunk-based:  main only + short branches + feature flags + continuous deployGitHub Flow:  branch → MR → review + CI → merge → deploy mainGitLab Flow:  main → staging → production (downstream only), or release/* branchesGitFlow:      develop, feature/*, release/*, hotfix/*, main = released versionsAlways:       protected main, required pipeline, reviews, small MRs, delete merged branches
OUTPUTterminalutf-8 · 5L

Key takeaways

  • A branching strategy defines which branches exist, how long they live and how changes reach production.
  • Trunk-based development and GitHub Flow keep branches short and main always releasable; they fit CI/CD best.
  • GitLab Flow adds environment or release branches for staged or versioned delivery; GitFlow suits scheduled, versioned products.
  • Whatever the strategy, protect main, require pipelines and reviews, and keep merge requests small.