Sub topic 5
A trunk-based flow with main as the source of truth
This chapter assumes you know basic Git (branches, merges, pull requests) and Salesforce metadata such as Apex classes and flows. It also assumes you know that sandboxes and production are separate orgs. The previous chapter compared GitFlow, trunk-based development, and environment-per-branch models. This chapter explains the model this tutorial recommends and the rules that make it work.
The core idea
Trunk-based development means the team keeps one long-lived branch, called the trunk, and does all work through short-lived branches that merge back into it. Here the trunk is main.
main is the source of truth. If you want to know what the team intends production to look like, you read main. No org holds that role, even though orgs are where the code runs.
This reverses a common habit. Many Salesforce teams treat production or a full sandbox as the real version and Git as a backup. In this flow, Git leads and orgs follow.
The rules of the model
The model rests on a few rules. Each one prevents a specific failure.
- Every change reaches
mainthrough a pull request. This gives you a place for review and validation. - Branches start from the latest
mainand are merged back within days, not weeks. Long branches drift frommain, and Salesforce metadata files conflict more often as they drift. - Only code that is ready to deploy is merged.
mainshould never contain work you would be afraid to release. - Environments are promoted from
main, never from a feature branch. Whatever sits in UAT or production came from a known commit onmain. - Hotfixes use the same path as features. They are only faster.
What main represents
Because main is the source of truth, you can answer three questions from Git alone:
- What is approved to ship?
- Which commit is deployed in each org?
- What changed between two releases?
The second question depends on tagging, which a later chapter covers. For now, notice that the model only works if main is a complete description of the project, so everything you deploy has to live in the repository.
Why this fits Salesforce
Salesforce adds pressures that ordinary application code does not have. The following table shows how this flow answers each one.
| Salesforce pressure | How the flow responds |
|---|---|
| Metadata files such as flows and profiles conflict easily | Short branches keep each diff small and recent |
| Several orgs exist (scratch, sandbox, UAT, production) | One branch feeds all of them, so no org-specific branches can diverge |
| Deployments run tests and can fail | Pull requests are validated before merge, so `main` stays deployable |
| Teams make urgent production fixes | A hotfix is an ordinary short branch, not a separate process |
Environment-per-branch models need a branch for each org, so the branches can disagree about what a feature is. A single main removes that class of problem.
Trade-offs
This model asks for discipline. Incomplete work cannot sit on main, so large features need to be split into small mergeable pieces. Unfinished behavior can be hidden until it is ready, for example with a custom permission or custom metadata flag. Without that habit, people fall back to long branches.
Check your understanding
Exercise
A teammate has a feature branch that is three weeks old. They propose deploying it straight to the UAT sandbox so testers can start, and merging to main later. Which rules of the trunk-based flow does this break, and what would you suggest instead?
Show answer
It breaks the rule that environments are promoted from main, so UAT would hold code that is not in the source of truth. It also breaks the short-lived branch rule, because a three-week branch is likely to conflict with main. Instead, split the work into small pieces, hide unfinished behavior behind a flag, merge each piece to main through a validated pull request, and promote main to UAT.