Trunk-based development

Intermediate · 14 min

Keep one main branch that is always releasable, merge small changes into it every day, and hide unfinished work behind a flag instead of a long-lived branch.

Before thisMerging: fast-forward and three-wayFetch, pull and push

Modern Software Engineering · 14 min

Trunk-based development

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Modern Software EngineeringReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Trunk-based development means everyone integrates into one main branch at least daily, using branches that live for hours or a day or two, not weeks.
  • The cost of merging grows faster than the size of the divergence, so frequent small merges are cheaper than rare big ones.
  • Unfinished work ships to main hidden behind a feature flag, which separates deploying code from releasing a feature.
  • The approach only works with fast automated tests, because main must stay releasable after every merge.
  • Slow code review is the usual way trunk-based development fails in practice, so review small changes quickly.

The problem with branches that live too long

Say you and a teammate each start a feature on Monday. You make a branch, they make a branch, and you both disappear into your work. Two weeks later you try to merge. In the meantime a third person renamed a function, someone reformatted a file, and your teammate rewrote half of the module you were also editing.

Nothing about this is a Git failure. Git can combine changes fine. The problem is that nobody saw anyone else’s work for two weeks, so every assumption you made went unchecked. Git reports the overlap as conflicts. The harder conflicts are the ones Git does not report: your code merges cleanly and then fails, because it called something the other branch changed.

Trunk-based development is the reaction to this. There is one shared branch (the trunk, usually called main). Everyone merges into it at least once a day. Branches still exist, but they are short: you make one, push a small change, get it reviewed, merge it, and delete it, often within a few hours.

Why small and frequent wins

The cost of a merge is not proportional to the size of the change. It grows faster than that, because the number of places two sets of changes can collide grows with both sets, and because your memory of why you wrote something fades.

Think of it like doing your accounts. Reconciling one day’s receipts takes five minutes. Reconciling a month of them takes an evening, and you will not remember what half the entries were. Small frequent merges keep the reconciliation cheap.

There is a second benefit. When main always contains everyone’s latest work, a bug that appears is much easier to pin to a recent small change. A tool like git bisect has a short, clean history to search.

How you hide unfinished work

The obvious objection: a feature takes a week, so how can you merge it every day? Merging half a feature to main would ship half a feature.

The answer is that merging and releasing are different things. You merge the code every day, but keep it switched off with a feature flag: a condition in the code that decides whether the new behaviour runs. Here is the idea for the running example, a small notes CLI where you are adding #tag extraction:

// src/flags.mjs — flags come from the environment, so a deploy can change them
export const flags = {
  tags: process.env.NOTES_FLAG_TAGS === "on",
};

// src/tags.mjs
import { flags } from "./flags.mjs";

export function extractTags(text) {
  if (!flags.tags) return [];
  return [...text.matchAll(/#([\w-]+)/g)].map((m) => m[1]);
}

I ran this with the flag off and on. With NOTES_FLAG_TAGS=off, extractTags("ship #urgent #v2 now") returns []. With NOTES_FLAG_TAGS=on it returns ['urgent', 'v2']. The code is on main either way. Flipping the flag later is a configuration change, not a merge. If the feature misbehaves in production, you switch it off without reverting anything.

Flags have a cost. Each one is a branch in your code that you have to test both ways, and old ones rot. Delete a flag once its feature is fully on.

What the pipeline has to provide

Trunk-based development moves the safety net from “isolation on a branch” to “automation on main”. Two things have to be true.

  • Fast tests. Every merge goes into the branch you release from, so the checks on a pull request must catch breakage, and they must finish in minutes. If the suite takes an hour, people batch their changes, branches grow, and you are back where you started. You will build this gate in the GitHub Actions group.
  • Quick review. A small change waiting two days for a reviewer is a two-day-old branch. Teams that succeed treat review as higher priority than starting new work, and keep changes small enough to review in a few minutes. Some teams skip pull requests for experienced pair-programmed work, but that is a choice about review, not about branching.

Branch protection (later in this group) is how you make “main must stay green” a rule instead of a hope.

What the video covers

Modern Software Engineering’s “We Tried Trunk-Based Development… The Results Were Shocking.” runs about 13 minutes and was published around December 2025 (YouTube shows only “11 months ago”, so the month is approximate). I could not read a transcript, so this summary rests on the title and the one-line description, which says trunk-based development has a reputation for being fast and effective and asks whether it works in the real world. The channel is the one run by Dave Farley, as I understand it, which argues for continuous integration as a core practice, so expect an argument for the approach rather than a neutral comparison. The title is click-bait style, but I judged the channel’s track record the better guide. I judged the language to be English from the title, description and channel alone. A second, older option is linked under deeper: a step-by-step walkthrough from around 2022 that shows the day-to-day mechanics.

Where this fits

Trunk-based development is one end of a spectrum. The other end gives each feature and each release its own long-lived branch, which is the next lesson: feature branches and Gitflow.

Check yourself

  1. 01Why does merging a branch that is two weeks old hurt more than merging two branches that are one day old?

    Everyone else's changes pile up on main in that time, so the two sides have drifted further apart. More files overlap, conflicts are harder to resolve, and you no longer remember every decision you made.

  2. 02What does a feature flag let you do that a long-lived branch does not?

    It lets half-finished code live on main and ship to production, switched off. Other people integrate against it every day, and turning the feature on later is a configuration change, not a giant merge.

  3. 03What must be true of your tests before trunk-based development is safe?

    They must run quickly and catch breakage reliably, since every merge goes straight into the branch you release from. If the tests are slow or flaky, people stop merging often.

  4. 04A team says it does trunk-based development, but each feature branch lives for three weeks. Is that right?

    No. The branch lifetime is the defining property. Three-week branches are feature branches with a different name, and they bring the late-merge pain trunk-based development exists to avoid.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark