Sub topic 11
Moving source to and from the scratch org
A scratch org starts nearly empty. It holds the features and settings from your definition file, and nothing else. Your code and metadata live in the project on your machine. This chapter covers how to move that source into the org, how to bring changes made in the org back, and where this goes wrong.
Prerequisites
This chapter assumes you have the following:
- A Salesforce DX project generated with
sf project generate, withsfdx-project.jsonin its root. - A scratch org created with an alias, here
my-scratch. - Salesforce CLI (
sf) installed and the Dev Hub already authorized. - Basic familiarity with metadata types such as Apex classes, custom objects, and Lightning web components.
How source moves
Your project keeps metadata in "source format". Each package directory listed in sfdx-project.json holds files under paths like force-app/main/default/classes. A deploy sends those files from your machine to the org. A retrieve copies metadata from the org into your project.
Scratch orgs also support source tracking. The org records which components changed in the org, and the CLI records which files changed locally. This lets the CLI send or fetch only the differences instead of the whole project.
Deploying local source
Run the deploy command from inside the project directory:
sf project deploy start --target-org my-scratchWith source tracking enabled, the command deploys only what changed since the last sync. On the first run, it deploys everything in your package directories. If you set a default org when you created the scratch org, you can leave out --target-org.
You can also deploy a limited set of files. This is useful when you only want to test one change:
sf project deploy start --source-dir force-app/main/default/classes --target-org my-scratch
sf project deploy start --metadata ApexClass:MyController --target-org my-scratch--source-dir takes a path to a file or folder. --metadata takes a type and a name. You can repeat either flag. When you use one of them, the CLI deploys exactly what you named and ignores the tracking-based change detection for that run.
Reading the result
A successful deploy prints a table of components with their state, such as Created, Changed, or Unchanged. A failed deploy prints the component name, line number, and error message. Fix the file and run the command again. Because the deploy is all-or-nothing by default, a failure in one component means none of the components from that request are saved.
Retrieving changes from the org
Sometimes you build in the org, for example with Setup, Object Manager, or Flow Builder. Those changes exist only in the org until you pull them down:
sf project retrieve start --target-org my-scratchWith source tracking, this retrieves components that changed in the org since the last sync. Review the new or modified files in your project and commit them to version control.
To see what the CLI thinks has changed before you move anything, use the preview commands:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchEach prints a table of components that would be deployed, retrieved, or that conflict. Use these as a habit before a retrieve, so you know what will land in your working tree.
Conflicts
A conflict happens when the same component changed both locally and in the org since the last sync. The CLI stops instead of overwriting either side, and reports the conflicting components.
You have two choices, and both discard some work:
- Add
--ignore-conflictsto the deploy to overwrite the org with your local version. - Add
--ignore-conflictsto the retrieve to overwrite your local files with the org version.
sf project deploy start --target-org my-scratch --ignore-conflicts
sf project retrieve start --target-org my-scratch --ignore-conflictsSource tracking and its limits
Tracking relies on a record kept in your project's local .sf folder, along with a record kept by the org. Several situations break or confuse it:
- Deleted
.sffolder or a fresh clone. The local record is gone. The CLI may treat everything as changed, or report conflicts. You can pass--source-dirfor the first deploy, or reset tracking (see below). - A different machine. The tracking record is local, so a teammate who clones the project does not inherit it. Each developer needs their own scratch org and their own record.
- Some metadata types. Not every type is tracked. If a change in the org does not show up in
retrieve preview, retrieve it by name with--metadata. - Deleted components. A component you delete in the org is reported as a deletion. Whether the local file is removed depends on the command and flags, so check the preview output and your version control status afterward.
To rebuild the tracking state, you can reset it:
sf project reset tracking --target-org my-scratchThis clears the local record and the org's change history for that org. After this, the next deploy or retrieve starts from a clean baseline. Reset only when your project and org already match, or you will lose the information about what differs.
Pushing and pulling: older commands
Older tutorials use sf project deploy start as "push" and sf project retrieve start as "pull" in a source tracking sense. Older CLI versions used separate commands for tracked and untracked orgs: force:source:push, force:source:pull, force:source:deploy, and force:source:retrieve. If you meet them, the current sf project deploy start and sf project retrieve start cover both cases. They detect that the target is a tracked org and behave accordingly.
| Task | Current command |
|---|---|
| Send local changes to the org | `sf project deploy start` |
| Bring org changes to the project | `sf project retrieve start` |
| Check what would be sent | `sf project deploy preview` |
| Check what would be fetched | `sf project retrieve preview` |
| Clear tracking state | `sf project reset tracking` |
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| "No source-backed components present" or nothing deployed | Wrong directory, or `packageDirectories` path in `sfdx-project.json` does not match your folders | Run from the project root and check the `path` entries |
| Conflicts reported on every run | Tracking record is stale or missing | Run the preview, then reset tracking if the project and org match |
| "Entity of type X named Y cannot be found" | A dependency is not in the org yet, or a needed feature is missing from the definition file | Deploy the dependency first, or add the feature to the scratch org definition and recreate the org |
| Deploy succeeds but a field is missing for users | Permissions are not granted | Deploy a permission set and assign it with `sf org assign permset` |
| Test or Apex compile errors | Code references metadata that is not in the project | Retrieve the missing component, or fix the reference |
Permissions deserve emphasis. A new custom field is visible in the org's metadata, but users can only see it if a profile or permission set grants access. Include a permission set in your source and assign it:
sf org assign permset --name My_Permission_Set --target-org my-scratchA practical workflow
- Create or open the scratch org and deploy the project.
- Make changes locally, or in the org through Setup.
- Before each sync, run the matching preview command.
- Deploy local changes, or retrieve org changes. Resolve conflicts deliberately.
- Commit the results to version control.
Keep one rule: decide which side is the source of truth for each component. Source code is usually edited locally. Declarative configuration is often easier to build in the org and then retrieve.
Exercise
You edited an Apex class locally. A teammate with admin access changed the same class in your scratch org through the Developer Console. You run sf project deploy start and the CLI reports a conflict. Which commands do you run first to see what is affected, and what happens if you then add --ignore-conflicts to the deploy?
Show answer
Run sf project deploy preview --target-org my-scratch and sf project retrieve preview --target-org my-scratch to list the components that differ and which are in conflict. Adding --ignore-conflicts to the deploy overwrites the org's version of the class with your local version, and the Developer Console edit is lost. If the org change matters, retrieve it first (or copy it out) and merge it by hand.
Exercise
A new custom field deploys successfully, but a test user cannot see it on the page layout. What is the most likely cause and the fix?
Show answer
The user has no field-level access, because deploying a field does not grant permissions. Add the field to a permission set in your source, deploy it, and assign it with sf org assign permset --name <PermSetName> --target-org my-scratch.