Sub topic 3
Mapping orgs to the places code lives
This chapter assumes you know basic Git (branches, merges, pull requests), you have seen Salesforce metadata such as Apex classes and flows, and you can run commands in a terminal. It explains where each kind of org fits in a Git-based flow. Later chapters build the branching model on top of this map.
Git is the source of truth, orgs are targets
In a Salesforce project, the Git repository holds the intended state of your metadata. An org is a place where that metadata runs. Production is the org your users depend on, so it should receive only changes that came from the repository. A sandbox is a copy of production used for development or testing. A scratch org is a temporary org created from a definition file, and you can delete it when you finish.
If you treat an org as the source of truth, work gets lost when someone refreshes or deletes it. If you treat Git as the source of truth, any org can be rebuilt or compared against it.
The org types and their roles
| Org type | Lifespan | Typical role | Where its code comes from |
|---|---|---|---|
| Scratch org | Days (1 to 30) | One developer, one feature | Deployed from a feature branch |
| Developer sandbox | Weeks or longer, refreshable | Work that needs production-like data or settings | Deployed from a feature branch |
| Partial or full sandbox (UAT) | Long-lived | Acceptance testing before release | Deployed from main |
| Production | Permanent | Live users | Deployed from main, usually from a tag |
The key difference is lifespan. Short-lived orgs pair with short-lived branches. Long-lived orgs such as UAT and production map to the long-lived branch, main.
Create a scratch org from the project
A scratch org is built from config/project-scratch-def.json. This file sets the edition, features, and settings.
{
"orgName": "Case routing scratch org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Because this file is committed to Git, every developer gets the same starting point.
sf org create scratch --definition-file config/project-scratch-def.json --alias case-routing --duration-days 7
sf project deploy start --target-org case-routingThe alias case-routing is a local nickname for the org. Aliases are stored on your machine, not in Git.
How the pieces connect
Changes flow in two directions in the first step. You deploy source to the org, build declarative changes there, and retrieve them back into the branch. Past main, the flow runs one way, from the repository outward.
Common errors
- Building in a shared sandbox. Two developers editing the same org overwrite each other. Give each person a scratch org or a personal developer sandbox.
- Editing production directly. The change exists only in the org. The next deployment from
mainmay overwrite it. Later chapters cover detecting this, called org drift. - Forgetting to retrieve. A flow or page layout built in the org is not in Git until you run
sf project retrieve start. If you delete the org first, the work is gone. - Committing credentials. Org auth details belong in your local CLI store or CI secrets, never in the repository.
Exercise
A teammate builds a new flow in a developer sandbox and then refreshes the sandbox the next day. The flow is missing. What step was skipped, and which command performs it?
Show answer
They never retrieved the flow into their feature branch and committed it. Running sf project retrieve start --target-org <alias> pulls the metadata into the local project, and git add and git commit then save it. The refresh replaced the sandbox with a fresh copy of production, so anything not in Git was lost.