Sub topic 4
Comparing Branching Models for Salesforce
This chapter compares three branching models that Salesforce teams commonly use: GitFlow, environment-per-branch, and trunk-based development. It ends with criteria for choosing one. The next chapter builds out the trunk-based flow in detail.
What this chapter assumes
You should already know how to branch, merge, and open a pull request. You should know what Apex classes, custom objects, and flows look like as files in a Salesforce DX project. You should also know that a team usually has a production org and several sandboxes. The earlier chapters on metadata as source and on mapping orgs to places code lives cover the background. Here they are used as given.
What a model has to handle on this platform
A branching model is a rule set for where work happens, where it is merged, and what each branch means. On most platforms, "what is on the branch" and "what is running" are linked by a build pipeline. On Salesforce the link is weaker, for these reasons:
- An org holds state that Git does not. Users, data, some settings, and anything changed through Setup that nobody retrieved exist only in the org. A branch describes metadata, not the org.
- Deployments are not undone by Git. Reverting a commit changes files. The org changes only when you deploy again. Deleting a file also does not delete the component in the org unless the deployment carries a destructive change.
- Production deployments are gated. Deploying Apex to production runs tests, and at least 75% of Apex must be covered. A branch that merges cleanly can still fail to deploy.
- Sandboxes are refreshed from production. A refresh overwrites what was in the sandbox. A sandbox that "was" a branch is back to production's state afterward.
- Metadata XML conflicts easily. Profiles, permission sets, flows, and layouts are large single files that many people edit. Branches that live a long time collide more often.
Each model below answers these constraints differently.
GitFlow
GitFlow uses several long-lived branches. main mirrors production, develop collects finished work, release/* branches stabilize a release, and feature/* and hotfix/* branches hold short-term work.
Teams adopt it because it looks like how Salesforce teams already think: a release train with a stabilization period and a UAT phase. The mapping is natural. develop is deployed to an integration sandbox, release/* to UAT, and main to production.
The costs show up in practice:
- Every change is merged at least twice (feature to
develop,developtorelease/*, release tomain, and back-merges). Every merge is a chance for an XML conflict. developandmaindrift apart. A fix made on arelease/*branch has to be merged back, and people forget.developis rarely deployable on its own. Half-finished features sit next to finished ones, so it is hard to ship one feature without the others.- Hotfixes need their own path and a back-merge, which is easy to get wrong.
GitFlow works best when releases are infrequent, scheduled, and large, and when a QA team needs a frozen release candidate for weeks. For teams that release every week or two, the overhead is hard to justify.
Environment-per-branch
In this model, each org has a branch: dev, qa, uat, main (production). Work moves forward by merging one branch into the next, and each merge triggers a deployment to the matching org.
The appeal is that the branch tells you what is in each org. Anyone can look at uat and know what UAT should contain.
flowchart LR
F[feature branch] --> D[dev branch]
D --> Q[qa branch]
Q --> U[uat branch]
U --> M[main branch]
D -.deploys to.-> DS[(Dev sandbox)]
Q -.deploys to.-> QS[(QA sandbox)]
U -.deploys to.-> US[(UAT sandbox)]
M -.deploys to.-> P[(Production)]The flowchart above is a plain-text description of the model, so the next block shows the diagram in its required form.
The main weakness is that what reaches production is not what was tested. Suppose a change passes in qa while an unrelated change is also in qa. When the team promotes, they merge the whole branch, or they cherry-pick. Merging the whole branch ships unrelated work. Cherry-picking creates commits in uat that are not the same commits tested in qa, so the history of the branches diverges.
You can see this divergence with Git:
# Commits in uat that are not in main (what a promotion would carry)
git log --oneline main..uat
# Commits in main that never passed through uat (a sign of bypassed promotion)
git log --oneline uat..mainIf the second command prints anything regularly, the branches no longer describe a pipeline. They describe a set of copies that have each been patched by hand.
Other problems:
- A sandbox refresh resets the org, but the branch keeps its old content. The two stop matching until someone redeploys.
- A bad change in
qablocks every change behind it, because they travel together. - Rollback is a new deployment from an older commit. Nothing about the branch structure helps.
This model fits teams that have no way to build a single release artifact and must test in a fixed sequence of orgs. It is the weakest choice when you want to know exactly what you tested.
Trunk-based development
In trunk-based development, there is one long-lived branch, main. Developers create short-lived branches, validate them, and merge them back to main quickly, often within a day or two. Environments are not branches. Instead, main is deployed to each environment in turn, and releases are marked with tags.
For Salesforce this has several effects:
- One history. A commit tested in UAT is the same commit that reaches production. There is no re-merge step in between.
- Smaller conflicts. Branches live for days, not weeks, so files like profiles and flows collide less.
- Validation before merge. A check-only deployment on each pull request shows whether the change would deploy, without saving anything in the org.
- Unfinished work stays out of the way. Work that is not ready to be visible is merged but switched off, for example behind a custom permission, a feature flag in custom metadata, or an inactive flow version.
The last point is the real cost. Trunk-based development asks the team to merge work that is not yet released. If you cannot control visibility in the org, you may not be able to adopt the model fully, and a half-adopted version drifts back toward long-lived branches.
Another cost is discipline. Without a gate, main can break. Pull request validation and branch protection are required, not optional. Later chapters cover both.
Side-by-side comparison
| Dimension | GitFlow | Environment-per-branch | Trunk-based |
|---|---|---|---|
| Long-lived branches | `main`, `develop`, release branches | One per org | `main` only |
| Meaning of a branch | A stage of a release | An org's contents | The source of truth |
| Merges per change | Several, plus back-merges | One per environment | One |
| Same commit tested and shipped | Mostly, through the release branch | Often not | Yes |
| Conflict exposure | High | High | Low |
| Needs ways to hide unfinished work | No | No | Yes |
| Typical release rhythm | Scheduled, large | Scheduled | Frequent, small |
| Main failure mode | Drift between `develop` and `main` | Untested combinations reach production | Broken `main` without a gate |
Common errors when choosing
- Choosing by org count. Having four sandboxes does not mean you need four branches. Orgs are deployment targets. Branches are lines of history.
- Treating a branch as proof of what is in an org. Sandboxes can be refreshed, and people change orgs directly. Only a deployment, or a retrieval and comparison, tells you what an org holds.
- Adopting trunk-based development without validation. Merging fast without a check-only deployment moves failures from the pull request to the production deployment.
- Keeping a "temporary" long-lived feature branch. A branch that lives for a month in a trunk-based team is a small GitFlow with all of its merge pain and none of its structure.
- Mixing models. A team that has
mainanduatas branches but also tags releases frommainhas two sources of truth and will eventually argue about which one is correct.
Decision criteria
Ask these questions in order:
- Can you hide unfinished work in the org (permissions, flags, inactive flows)? If yes, trunk-based is available to you. If not, decide how to solve that first.
- Do you need a frozen release candidate for a long certification or audit window? If yes, a short-lived
release/*branch cut frommainis an option, and it is not the same thing as GitFlow's permanentdevelop. - Does your tooling deploy from a branch only, with no option to deploy a tag or commit? That limits trunk-based flow, and you may need a pipeline change.
- How many people edit the same metadata files? More people on the same files makes long-lived branches more costly.
For most teams that release at least monthly, trunk-based development with main as the source of truth has the lowest cost and the clearest history. That is the model the rest of this tutorial builds.
Exercise
Your team has dev, qa, uat, and main branches, each deployed to one org. Running git log --oneline uat..main prints three commits. What does that tell you about the pipeline, and why is it a problem for knowing what was tested?
Show answer
The three commits are in main but were never merged through uat, so someone changed production's branch directly, most likely with a hotfix or a cherry-pick. The next promotion from uat will not include them, and those changes were not tested in UAT as part of the sequence. The branches have stopped describing a pipeline, so you can no longer say that what reached production is what was tested. A single-history trunk-based flow avoids this because the same commit moves through every environment.