Catalog My learning Blog
CI/CD and DevOps Tutorial: Build a Delivery Pipeline · Phase 1 · Lesson 2 · 3 min read

Trunk-Based Development vs GitFlow

On this page
  1. Why long-lived branches hurt
  2. How GitFlow works
  3. How trunk-based development works
  4. Merging unfinished work behind feature flags
  5. Making trunk-based development work
  6. Check yourself

Why long-lived branches hurt

Two developers spend three weeks on separate feature branches. When they merge, their changes conflict in a dozen files, the tests that passed on each branch fail together, and nobody remembers why some lines changed. The longer branches live, the more they drift apart, and the later the team finds out.

Picture two people editing copies of the same document offline. If they sync every hour, each sync is a few small, obvious differences. If they sync after three weeks, they face hundreds of overlapping edits, and neither remembers why half of them were made. Branching strategy is mostly a decision about how often you sync.

How GitFlow works

GitFlow (Vincent Driessen, 2010) uses several long-lived branches:

  • main holds released code; develop collects finished features.
  • feature/* branches come off develop and can live for weeks.
  • release/* branches stabilize a version; hotfix/* branches patch production.

It suits software shipped as versions (installed apps, libraries, firmware) where several releases are maintained at once. For a web service deployed continuously, its author later noted it's often more process than needed: work waits in branches, and integration happens late.

How trunk-based development works

In trunk-based development, everyone integrates into one branch, main, at least daily:

  • Branches are short-lived: hours to a day or two, merged through small pull requests.
  • main is always releasable, protected by CI (Day 3).
  • Releases come from main, by tag or by continuous deployment.
GitFlow keeps long-lived develop, feature and release branches; trunk-based development merges small branches into main every day.
GitFlow keeps long-lived develop, feature and release branches; trunk-based development merges small branches into main every day.
GitFlowTrunk-based
Branch lifetimeDays to weeksHours to a day or two
IntegrationWhen a feature is doneContinuously
Merge conflictsLarge and lateSmall and early
Unfinished featuresHidden in branchesHidden behind feature flags
FitsVersioned products, several supported releasesWeb services, continuous delivery

DORA's research links trunk-based development (few active branches, merged at least daily) with higher delivery performance.

Merging unfinished work behind feature flags

"Merge daily" raises the obvious question: what about a feature that takes three weeks? You merge it in small, safe steps, hidden behind a feature flag (Day 15) until it's ready:

Shell
git switch -c checkout-layout main
# small change: new layout component, not used yet, behind a flag that's off by default
git commit -am "Add new checkout layout behind new-checkout flag (off)"
git push -u origin checkout-layout
gh pr create --base main --title "Checkout redesign 1/6: layout behind flag"
# reviewed and merged the same day

Each step is reviewed, tested against everyone else's latest code, and deployed, but users don't see it. Other techniques help too:

  • Branch by abstraction: introduce an interface, build the new implementation behind it, switch over, delete the old one.
  • Dark launches: run new code paths in production without showing results, to test performance.

Making trunk-based development work

  • Fast CI (under about 10 minutes) so small merges aren't painful (Days 12 and 13).
  • Small pull requests that reviewers can read in minutes.
  • Branch protection: required checks and reviews on main.
  • A merge queue for busy repositories, which tests each pull request against the latest main before merging, so main stays green.

Check yourself

Each branch drifts away from main and from other branches every day. Conflicts and broken interactions between changes pile up unseen and all surface at merge time, when they're hardest to understand and fix.

Merge it into main in small, working increments, at least daily, hidden behind a feature flag or behind an abstraction that keeps the old path in use. Users see nothing until the feature is complete and the flag is turned on.

For software released as numbered versions, such as installed apps, libraries or firmware, especially when several versions must be maintained and patched at once. For a continuously deployed web service, its extra branches mostly add delay.

Small enough to review in minutes and merge within a day, usually a few hundred lines at most. Small changes are easier to review, test and roll back.

Introducing an interface in front of the code being replaced, building the new implementation behind it in main, then switching over and removing the old one.