Sub topic 1
Why Salesforce changes the branching question
Prerequisites
This chapter assumes you already have the following:
- Basic Git use: committing, branching, merging, and opening a pull request.
- Familiarity with Salesforce metadata such as Apex classes, custom objects, and flows.
- Awareness that Salesforce work happens in sandboxes and a production org.
- Basic command-line use. You will see a few
sfandgitcommands.
You do not need to know any branching model by name yet. The later chapters compare them. This chapter explains why the usual advice for application code does not carry over unchanged.
The usual branching advice assumes a build
Most branching guides come from ordinary application development. In that setting, the repository is the only place the program exists. A build server compiles the code, produces an artifact, and a deploy step copies that artifact to a server. If a server breaks, you rebuild it from Git.
That assumption gives you several guarantees:
- The repository holds the full definition of the product.
- Every environment runs an artifact built from a known commit.
- Nobody edits the running server by hand, or if they do, it is treated as an incident.
A Salesforce org breaks each of these guarantees. To choose a branching strategy, you need to know how.
Metadata is the source, and the org is its running copy
On Salesforce, the things you build are described as metadata. Metadata is the configuration and code that define an org: Apex classes, triggers, custom objects and fields, flows, page layouts, permission sets, and so on. With Salesforce DX, each item is stored as a text file in your repository, mostly XML, and the Metadata API turns those files into org changes.
A custom field is a typical example. This file lives at force-app/main/default/objects/Account/fields/Region__c.field-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Region__c</fullName>
<externalId>false</externalId>
<label>Region</label>
<length>40</length>
<required>false</required>
<type>Text</type>
<unique>false</unique>
</CustomField>Treating this file as source code is the idea behind everything in this tutorial. Once the field is a file, you can review it in a pull request, see who changed it, and revert it. The file is not the product, though. The product is the configuration inside a live org, and that is where the differences begin.
Five ways Salesforce differs from a typical codebase
1. People change the org without touching Git
Admins build fields, flows, validation rules, and layouts in Setup. These changes are valid and often the quickest way to deliver value. They happen in the org first, and Git only learns about them if someone retrieves the change. In a normal application, a hand edit on the server is the exception. In Salesforce, a hand edit in a sandbox is routine.
This means the repository can fall behind the org it describes. A later chapter covers detecting and reconciling this org drift. For now, note the consequence for branching: a branch is not the only place work happens, so the strategy must say how work done in an org reaches a branch.
2. There is no build artifact
A deployment sends metadata files to an org, and the org compiles Apex and applies configuration. You do not produce a binary that you can later promote unchanged. Each target org receives the source again and processes it. Two orgs with different contents or settings can react differently to the same commit.
For branching, this means you validate a commit by deploying it to an org, not by building it. A pull request check needs a real org to run against.
3. Orgs are shared and stateful
Each environment has its own data and configuration, and some of it never appears in Git. A sandbox might hold half-finished work from three people. Production contains settings and records that no repository describes. Two branches cannot each have their own copy of a shared sandbox the way they can each have their own checkout of the code.
The practical effect is that environments become a limited resource that your branching model has to schedule or avoid. Scratch orgs and developer sandboxes reduce the problem by giving each developer an isolated org. The next chapters cover how orgs map to places where code lives.
4. Deploys depend on order and on the target
Metadata has dependencies. A field must exist before a layout can reference it, and a permission set can reference a field that has to be deployed first. Some metadata also changes meaning depending on the org. Production deployments that include Apex must run tests and meet at least 75% overall Apex code coverage, so a change that deploys cleanly to a sandbox can fail validation in production.
This pushes you to validate against an org that resembles the target, and to do it before merging, not after.
5. Metadata files merge badly
Git merges text by lines. Salesforce XML often puts many unrelated settings into one file, and the platform controls the format and ordering when it exports a file. This has three effects:
- A profile or permission set collects field, object, and class access in a single file. Two developers adding unrelated permissions tend to edit the same area.
- A flow is stored as one XML document. A small change in the Flow Builder canvas can reorder or rewrite large parts of it, which makes diffs hard to read.
- Some files, such as page layouts and custom object definitions, are large, and many people touch them.
The result is more merge conflicts than in typical application code, and conflicts that are harder to resolve safely. Long-lived branches make this worse, because two branches that diverge for weeks both rewrite the same files. A later chapter covers resolving these conflicts. Keeping branches short is the main way to reduce how often they happen.
Seeing the difference
The diagram contrasts the two flows. In ordinary application development, changes travel in one direction. In Salesforce, changes can enter the org from two directions, and the org is where validation happens.
The retrieve arrow is the part that has no counterpart in application development. Your strategy needs a rule for when it happens and who does it.
What this means for a branching strategy
These differences lead to a short list of requirements that the rest of the tutorial builds on:
| Salesforce fact | Requirement for the strategy |
|---|---|
| Changes are made in orgs as well as in Git | A defined way to retrieve declarative work into a branch |
| No build artifact | Validation by check-only deployment to a real org |
| Shared, stateful orgs | Isolated orgs for development, such as scratch orgs |
| Production tests and ordering rules | Validation before merge, against a production-like org |
| XML files that merge poorly | Short-lived branches and small, frequent merges |
A quick look at retrieve
Here is a small example of the retrieve step. An admin has added Region__c to Account in a developer sandbox. The developer pulls it into a branch and checks what Git sees:
sf project retrieve start --metadata "CustomField:Account.Region__c" --target-org dev-sandbox
git status --short
git diff --statGit now shows the new Region__c.field-meta.xml file as untracked. Before this command ran, the repository did not know the field existed. If the developer had instead deployed from an out-of-date branch back to the sandbox, the deploy would not have removed the field, because deploys do not delete metadata by default. The org and the repository would keep disagreeing without any error.
Common mistakes at this stage
- Treating Git as the whole truth. If admins build in orgs, the repository lags behind unless retrieval is part of the routine.
- Sharing one sandbox among many branches. Deploys from different branches overwrite each other, and you can no longer tell which branch produced the behavior you see.
- Letting branches live for weeks. Long-lived branches and profile or flow files produce painful conflicts.
- Validating only after merge. Without a check-only deployment on the pull request, a bad merge is found when it blocks everyone.
Exercise
A teammate says, "Our repository is the source of truth, so a green deploy to the shared QA sandbox from any feature branch means we are ready for production." Name two reasons from this chapter why that claim is unreliable.
Show answer
Any two of the following are correct. (1) Admins can change metadata directly in orgs, so the repository may not match the org, and the org may contain changes Git has never seen. (2) A shared sandbox is stateful, so deploys from different branches overwrite each other and a success does not show which branch caused the result. (3) Production runs Apex tests and requires at least 75% code coverage, and its data and settings differ from the sandbox, so a sandbox deploy does not prove production readiness. (4) Deploys do not delete metadata by default, so removed components may still exist in the sandbox and hide a problem.
Exercise
Why does a long-lived feature branch cause more trouble in a Salesforce project than in a typical application project?
Show answer
Salesforce metadata files such as profiles, permission sets, and flows combine many unrelated settings in one XML file, and the platform controls their formatting and ordering. Two branches that diverge for a long time are therefore likely to edit the same file regions and to produce conflicts that are difficult to read and resolve. Short-lived branches limit how far each branch drifts from main.