Sub topic 7
Deploying only your local changes
This chapter assumes you can work in a Salesforce DX project (sfdx-project.json, force-app), you have the sf CLI installed, and you have authorized an org. It also assumes you know the earlier chapters' ideas: tracked orgs such as scratch orgs record changes, and .forceignore keeps files out of that record.
What "deploy local changes" means
In a tracked org, sf project deploy start with no source flags does not send the whole project. The CLI compares your local files against the snapshot it stored the last time the project and the org were in sync. It deploys only the components that differ.
This has three practical effects:
- Deploys are faster, because the payload is a few components instead of hundreds.
- You do not have to list what you changed. The CLI computes it.
- Local deletions are included. If you deleted a tracked file, the CLI removes that component from the org too.
The basic loop
Create a tracked org if you do not already have one.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultEdit a file, for example force-app/main/default/classes/MyClass.cls. Then preview and deploy.
sf project deploy preview --target-org my-scratch
sf project deploy start --target-org my-scratchThe preview lists what would deploy, what would be retrieved, and any conflicts. It changes nothing. Running it first is a cheap habit that prevents most surprises. The deploy then sends only the components the preview listed under deploys.
The deploy command builds its payload in four steps:
- Read the local tracking state for the target org.
- Compare the current files against the stored snapshot, skipping anything matched by
.forceignore. - Check whether the org changed the same components since the last sync. If it did, report a conflict and stop.
- Send the changed components, then update the tracking state for the ones that succeeded.
What happens to tracking after a deploy
Tracking is updated per component, not per command. Components that deploy successfully are recorded as synchronized. Components that fail stay pending. Run the command again after you fix the error, and only the failed components and anything you edited since will go out.
Two consequences are worth knowing:
- A deploy that fails entirely leaves tracking untouched. Nothing is lost.
- A deploy that you only validate does not update tracking. See the dry run section below.
Dry runs
Use --dry-run to run the full deployment against the org, including compile checks and tests you request, without saving changes.
sf project deploy start --target-org my-scratch --dry-runBecause nothing is saved, the tracking state does not move. The same components still show as pending afterward. This is the correct behavior, but it means a successful dry run is not the same as being synchronized.
Choosing a specific scope even in a tracked org
You can still pass source flags in a tracked org. They override the "changed only" default.
sf project deploy start --target-org my-scratch --source-dir force-app/main/default/classes
sf project deploy start --target-org my-scratch --metadata ApexClass:MyClass
sf project deploy start --target-org my-scratch --manifest manifest/package.xmlWith these flags, the CLI deploys the components you named whether or not they changed. This is useful when you want to force a re-deploy of one class, or when the snapshot is suspect. Tracking is still updated for the components that deploy.
The table summarizes the choices.
| Command shape | Tracked org | Untracked org |
|---|---|---|
| No source flags | Deploys only local changes | Not meaningful, because the CLI cannot compute changes |
| `--source-dir` | Deploys everything in the path | Deploys everything in the path |
| `--metadata` | Deploys the named components | Deploys the named components |
| `--manifest` | Deploys what the manifest lists | Deploys what the manifest lists |
Conflicts at deploy time
A conflict exists when the same component has changed both locally and in the org since the last sync. The deploy stops and lists the conflicting components. It does not partially deploy the rest, so you get a clean failure instead of a mixed state.
You have two sound options.
- Retrieve first. Run
sf project retrieve preview, thensf project retrieve start. Merge the org's version with your local file, then deploy. - Overwrite the org on purpose with
--ignore-conflicts.
sf project deploy start --target-org my-scratch --ignore-conflictsResolving conflicts is covered in detail in a later chapter. Here the point is that the deploy command is the place where a conflict becomes visible, and the flag is a deliberate override.
Deleting components through a deploy
In a tracked org, deleting a file locally marks that component for deletion. The next deploy removes it from the org. Renaming a file is treated as a delete of the old name plus an add of the new one, so both sides show up in the preview.
Check the preview before deploying after deletions. A file removed by accident, for example by a bad branch checkout, will be removed from the org on the next deploy.
Test levels and long deploys
Tracked deploys accept the same options as other deploys. Two are worth remembering.
sf project deploy start --target-org my-scratch --test-level RunLocalTests --wait 30
sf project deploy start --target-org my-scratch --async--test-levelcontrols which Apex tests run. Scratch orgs and sandboxes accept any level. Production requires tests for Apex deploys, so plan for that when you move away from tracked orgs.--waitsets how many minutes the command waits. If the wait ends before the deploy finishes, the deploy keeps running in the org. Usesf project deploy reportorsf project deploy resumeto follow it.--asyncreturns right away.
Tracking is updated when the CLI learns the result. If you leave a deploy running and never check it, the local snapshot may lag behind the org until you report or resume it.
Common mistakes
- Expecting a tracked deploy to send everything. If your edit does not appear in the preview, check
.forceignorefirst, then confirm you saved the file under the package directory listed insfdx-project.json. - Using
--source-diras the default habit. It works, but it bypasses the "only what changed" benefit and sends more than needed. - Reaching for
--ignore-conflictsto make an error disappear. The error was telling you someone else changed the component. - Assuming a dry run synchronizes. It does not update tracking.
- Deploying to an org without tracking and expecting a diff. Sandboxes and production do not track by default, so you must name what to deploy.
Summary of the working model
A tracked deploy is a three-way decision: what you changed, what the org changed, and what was last in sync. The CLI sends your changes only when the org has not changed the same components. It records success per component. When you pass source flags, you replace the computed scope with your own.
Exercise
You edit one Apex class in a tracked scratch org project, then run sf project deploy start --target-org my-scratch --dry-run and it succeeds. You immediately run sf project deploy preview --target-org my-scratch. Will the class still be listed as a pending deploy, and why?
Show answer
Yes. A dry run validates the deployment but saves nothing in the org, and it does not update the tracking state. The class remains a local change that has not been synchronized, so the preview still lists it until you run a real deploy.
Exercise
A teammate changed MyClass in the org through Setup while you changed the same class locally. You run sf project deploy start --target-org my-scratch and it stops. What are the two deliberate ways to proceed, and what is the risk of the second?
Show answer
First, retrieve the org's version with sf project retrieve start, merge it with your local file, then deploy again. Second, rerun the deploy with --ignore-conflicts. The risk is that your version overwrites the teammate's org-side change without any merge, and their work is lost.