# Trunk-Based Development: How High-Velocity Teams Stay Out of Merge Hell

Most branching strategies are about managing the *separation* between people's work. Trunk-Based Development (TBD) flips that: it asks how you can minimize separation altogether.

The answer is blunt. Everyone integrates into one shared branch, `main` (the "trunk"), at least once a day. Branches, if they exist at all, live for hours rather than weeks.

This is how Google, Meta, and many other large engineering orgs work, and it's strongly correlated with high performance in the DORA research (deployment frequency, lead time, change failure rate, time to restore). It's not magic. It just removes the biggest source of integration pain: long-lived divergence.

## Two flavours

### 1\. Direct-to-trunk (small teams)

Developers commit straight to `main`, usually in pairs or after local tests pass. It works well for small, high-trust teams with excellent automated testing.

### 2\. Short-lived feature branches (most teams)

Developers branch off `main`, work for a few hours to a day or two, open a small PR, get a quick review, and merge. This is the realistic option for most organizations.

![](https://cdn.hashnode.com/uploads/covers/6a11b66c1a3cf7bffc297b8a/4c6002b5-961a-4ecd-bc4c-986bf4e4c35a.png align="center")

Compare that to GitFlow's diagram. Almost everything happens on one line.

## The rules of the road

1. **Integrate at least daily.** If your branch is older than two days, something's wrong.
2. **Keep** `main` **always releasable.** A broken build is the team's top priority, not one person's problem.
3. **Small, incremental commits.** Large changes get split into safe slices.
4. **Hide unfinished work behind feature flags**, not branches.
5. **Run fast, trustworthy CI** on every commit.
6. **Release from trunk**, or from short release branches cut off trunk (more on that below).

## The part everyone asks about: "How do I ship half-done features?"

This is where TBD either clicks or falls apart. The tools:

### Feature flags

Merge the code, but keep it dark until it's ready.

```js
// simple example
if (flags.isEnabled("new-checkout", user)) {
  return renderNewCheckout();
}
return renderLegacyCheckout();
```

Roll out gradually: internal users → 1% → 10% → everyone. If something goes wrong, you flip a flag instead of redeploying.

### Branch by abstraction

For large refactors (say, swapping out a payment provider), you introduce an abstraction, route calls through it, build the new implementation behind it, and flip over when ready. No long-lived branch required.

![](https://cdn.hashnode.com/uploads/covers/6a11b66c1a3cf7bffc297b8a/a9433d6d-7510-4868-8653-90fb45929661.png align="center")

Steps in practice:

1. Add an interface around the old code
2. Move callers to use the interface
3. Build the new implementation behind the same interface
4. Gradually switch traffic via a flag
5. Delete the old implementation and the flag

### Dark launches & keystone interfaces

Ship backend code with no UI that exposes it. The "keystone" (the button, route, or menu item) goes in last, and only then does the feature become visible.

## What about releases?

Two common approaches:

**Release from trunk.** Every green commit on `main` can go to production. Tag when you ship. This is the continuous deployment model.

**Short-lived release branches.** If you need a stabilization window or must patch released versions, cut a branch off trunk at release time:

```bash
git checkout main
git checkout -b release/1.2
git tag -a v1.2.0 -m "Release 1.2.0"
```

Critical rule: **fixes go to trunk first, then cherry-pick to the release branch**, never the other way around:

```bash
# fix merged to main as abc1234
git checkout release/1.2
git cherry-pick -x abc1234
git tag -a v1.2.1 -m "Patch 1.2.1"
```

Release branches get deleted when that version is out of support. They never merge back.

## What your pipeline needs to look like

TBD fails without strong CI. A reasonable shape:

![](https://cdn.hashnode.com/uploads/covers/6a11b66c1a3cf7bffc297b8a/2f53e640-ecd0-4905-8185-2e96af4f2e6a.png align="center")

Target: PR feedback in **under ~10 minutes**. If your pipeline takes an hour, people will batch changes to avoid waiting, and that's the exact behaviour you're trying to eliminate.

Some practical tricks for staying fast:

- Run only affected tests on PRs (monorepo tools like Nx, Bazel, or Turborepo help)
- Parallelize test suites
- Cache dependencies aggressively
- Quarantine flaky tests immediately instead of retrying forever

## Pros

- **Minimal merge conflicts.** Short-lived changes rarely collide badly.
- **Fast feedback and fast delivery.** Smaller changes, quicker reviews, more frequent releases.
- **Lower risk per change.** A 50-line change is easier to reason about (and revert) than a 5,000-line one.
- **Simple mental model.** One main line, little branching overhead.
- **Encourages good engineering habits:** testing, modular design, and feature flags.

## Cons

- **Needs mature CI/CD and testing.** Without it, you're just pushing bugs faster.
- **Feature flag debt is real.** Old flags pile up and make code confusing. Assign owners and expiry dates.
- **Cultural shift.** Developers used to "finish it, then merge" often find daily integration uncomfortable.
- **Weak fit for open-source with untrusted contributors**, where forks and long-lived PRs are the norm.
- **Not ideal for strictly versioned, multi-release products** unless combined with release branches.
- **Higher blast radius if the safety net is weak.** A bad commit reaches everyone quickly.

## Best practices

- **Merge at least once a day.** Make it a team norm, not a suggestion.
- **Pair branch protection with fast checks.** Require CI and one reviewer, but keep review turnaround to hours, not days.
- **Treat a broken trunk as an incident.** Revert first, investigate after:

  ```bash
  git revert <bad-commit>
  ```
- **Use pre-commit hooks and local test runs** so most issues are caught before CI.
- **Track flag lifecycle.** Every flag gets an owner and a removal ticket created at the time you add it.
- **Prefer small PRs.** If it takes more than ~30 minutes to review, it's probably too big.
- **Invest in observability.** Metrics, logs, and alerts let you spot regressions quickly when changes ship constantly.
- **Practice rollbacks.** Reverting should be boring.
- **Use progressive delivery:** canary deployments, percentage rollouts, automatic rollback on error spikes.

## Getting started without a big-bang change

If you're coming from GitFlow, don't flip everything overnight:

1. Cap feature branch lifetime (start with 3 days, then reduce)
2. Introduce feature flags for the next big feature
3. Speed up CI until PR feedback is reasonably quick
4. Merge `develop` and `main` into one branch
5. Drop release branches unless you genuinely need them

## Wrapping up

Trunk-Based Development is less a branching strategy than a discipline: integrate constantly, keep things shippable, and use flags instead of branches to manage incomplete work. It rewards teams that invest in testing and automation, and it punishes those that don't. If your team's ready, the speed gains are hard to beat.

---

Enjoyed this one? Buy me a coffee and help me keep writing: [**buymeacoffee.com/vatul16**](https://buymeacoffee.com/vatul16) ☕

Thanks for reading!
