Sub topic 11
Moving source to and from the scratch org
This chapter assumes you have a Salesforce DX project, an authorized Dev Hub, and a scratch org with the alias my-scratch. It also assumes you can run sf commands in a terminal.
Why scratch orgs track source
A scratch org has source tracking turned on. The org and the CLI both record which components changed since the last sync. Because of this, the CLI can send only your local edits to the org, and bring back only the edits made in the org. Sandboxes and production orgs don't track changes by default, so there you have to name what to move.
The project files in force-app are the source of truth. The scratch org is a disposable copy of that source.
Deploying local changes
Run this from the project folder:
sf project deploy start --target-org my-scratchThe first run deploys everything in your package directories, because nothing has been synced yet. Later runs deploy only the files you changed. To deploy a specific folder, add --source-dir force-app/main/default/classes. When you pass --source-dir, the CLI sends exactly those paths and ignores tracking.
Retrieving changes made in the org
Suppose you add a custom field in the scratch org's Setup. The local project doesn't have it yet. Pull it down:
sf project retrieve start --target-org my-scratchThe CLI fetches only components that changed in the org, and writes them into your project. Commit them to version control so the change isn't lost when the scratch org expires.
Previewing before you move anything
Preview commands list what would move, without moving it:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe output groups components as changes to deploy or retrieve, and as conflicts.
Resolving conflicts
A conflict occurs when the same component changed both locally and in the org since the last sync. The CLI stops the operation instead of overwriting either side. You have three options:
- Review both versions, merge them by hand in the project, and deploy again.
- Add
--ignore-conflictsto let the command overwrite the other side. - Discard the org's version by deploying with
--ignore-conflicts, or discard your local version by retrieving with it.
sf project deploy start --target-org my-scratch --ignore-conflictsCommon problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing deploys | No local changes since the last sync | Run `sf project deploy preview` to confirm, or pass `--source-dir` |
| Conflicts reported | Same component edited in both places | Preview, merge, or use `--ignore-conflicts` |
| Deploy fails on a component | Missing dependency or a feature absent from the org | Check the scratch definition file's `features` and `settings` |
| Tracking looks wrong | Tracking state is out of step with the org | Run `sf project reset tracking --target-org my-scratch` |
Exercise
You edited an Apex class locally, and a teammate edited the same class in the shared scratch org. You run sf project deploy start --target-org my-scratch. What happens, and what is the safest next step?
Show answer
The deploy stops and reports a conflict, because the same component changed on both sides since the last sync. The safest next step is to run sf project retrieve preview and sf project deploy preview to see the conflict, retrieve or copy the org's version, merge it with your local file, and then deploy again. Use --ignore-conflicts only if you intend to overwrite the other version.