Sub topic 11
Trunk-based development
This chapter assumes you know basic Git commands, how a branch points to a commit, and how pull requests and merges work on a host such as GitHub. It also assumes you have read the GitHub Flow chapter.
The working model
Trunk-based development is a workflow where everyone integrates into one shared branch, the trunk, usually main. Branches are allowed, but they are short-lived. A typical branch lasts hours, or a day or two at most, before it merges back.
The goal is continuous integration in the literal sense: every developer's work meets everyone else's work often. Small, frequent merges keep conflicts small and keep the trunk close to what each person has locally.
GitHub Flow is close to this model. The difference is the emphasis. Trunk-based development sets a hard expectation on branch age and requires that the trunk is always releasable, because it may be deployed at any time.
Two ways to work
Small teams sometimes commit straight to the trunk. This needs strong automated tests and discipline, because nothing reviews the change before it lands.
Larger teams use short-lived branches with a pull request. The branch is small, review is quick, and CI runs before the merge.
git switch main
git pull --ff-only
git switch -c add-search-box
# make a small change
git add .
git commit -m "Add search box markup"
git fetch origin
git rebase origin/main
git push -u origin add-search-boxRebasing just before you push keeps the branch current with the trunk. Open a pull request, get a review, and merge. Then delete the branch.
What makes it work
Three practices carry the model.
- Small changes. A change that takes five minutes to review gets reviewed. A change that takes an hour sits waiting, and the branch ages.
- Fast automated checks. The trunk stays releasable only if tests run on every change. A slow or flaky test suite pushes people toward long branches.
- Unfinished work hidden behind flags. You cannot finish a large feature in one day. Instead, merge it in pieces and keep it switched off. The next chapter covers feature flags in detail.
Common errors
- Branches that quietly grow. A "short-lived" branch that reaches a week has become a long-lived one. Split the work, or merge the finished part behind a flag.
- A broken trunk left alone. If a merge breaks the build, fix or revert it first. Reverting is usually faster than debugging under pressure.
- Treating review as a gate for large changes. Large pull requests get skimmed. Keep them small.
- No checks before merge. Without required checks, trunk-based development turns into frequent breakage.
When it fits
It suits teams that deploy often, have good test coverage, and can use feature flags. It fits less well when you must support several released versions at once. In that case you also need release branches, which a later chapter covers.
Exercise
A developer's branch has been open for six days and now conflicts with main. The feature is not finished. Under trunk-based development, what should they do?
Show answer
Split the work into small pieces, rebase on origin/main, and merge each finished piece to the trunk quickly. Hide the incomplete behavior behind a feature flag so the trunk stays releasable. Long-lived branches are the thing this workflow avoids.