Sub topic 6
Pushing, Pulling, and Source Tracking
This chapter explains how Salesforce CLI keeps your local project and a scratch org in sync. It covers what source tracking records, which commands use it, how conflicts are detected, and where tracking stops working. It assumes you know the basics of Salesforce metadata, can run commands in a terminal, and understand Git branches and commits.
Why source tracking exists
In source-driven development, the version control repository is the source of truth for configuration and code. The org is a disposable place to run and test that source. Developers still make changes in both places. They edit Apex in an editor, and they build objects, fields, and flows with point-and-click tools in Setup.
Source tracking is the CLI feature that follows those changes for you. It compares your local project files with the org and reports what changed on each side. Without it, you would have to remember which components you edited, or deploy and retrieve everything every time.
Tracking is enabled by default for scratch orgs. It is also available for Developer and Developer Pro sandboxes, but you must turn it on in Setup first. Partial Copy and Full sandboxes, and production, do not support it.
| Org type | Source tracking | Notes |
|---|---|---|
| Scratch org | On by default | Created from a Dev Hub |
| Developer sandbox | Optional | Enable in Setup, then create or refresh the sandbox |
| Developer Pro sandbox | Optional | Same as Developer |
| Partial Copy sandbox | Not supported | Use full deploy and retrieve |
| Full sandbox | Not supported | Use full deploy and retrieve |
| Production | Not supported | Deploy through a pipeline |
What the CLI records
For a tracked org, the CLI keeps a local record of the org's revision history, stored under your project's .sf folder (older projects may use .sfdx). Salesforce assigns a revision number to each change to a tracked component in the org. The CLI remembers the last revision it synced for every component, and it also remembers a fingerprint of the local file from the last sync.
From these two records, the CLI can classify each component:
- Local change: the file on disk differs from the last synced version. The next push or deploy will send it.
- Remote change: the org has a newer revision than the last one the CLI saw. The next pull or retrieve will bring it down.
- Conflict: both sides changed since the last sync.
Because the record lives on your machine, it is tied to one clone of the project and one org. A fresh clone of the same repository on another computer starts with no tracking history for the org.
Checking status
Run the status command to see the classification before you move anything:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe deploy preview lists components that a deploy would send, and flags conflicts. The retrieve preview lists components that a retrieve would bring down. Both are read-only. Run them often, especially before you pull, so no change surprises you.
Pushing local changes to the org
You send local changes with the deploy command. When the target org has tracking, the command sends only what changed locally:
sf project deploy start --target-org my-scratchThis is the command used in the earlier scratch org sequence. On a tracked org, it behaves as a push. If you name specific files, the CLI deploys only those, and the next run no longer reports them as changed:
sf project deploy start --source-dir force-app/main/default/classes/AccountService.cls --target-org my-scratchOn an untracked org, such as a Full sandbox, the same command deploys everything you point it at. It has no way to skip unchanged files. You control the scope with --source-dir, --metadata, or --manifest.
The deploy runs validation on the server. If a component fails, for example an Apex compile error, the CLI reports the error and the component is not applied. Fix the file locally and run the command again.
Pulling org changes into the project
Changes made in Setup exist only in the org until you retrieve them:
sf project retrieve start --target-org my-scratchOn a tracked org, the CLI retrieves only the components with remote changes and writes them into your package directory, such as force-app. Then you review the result with Git:
git status
git diff
git add force-app
git commit -m "Add Region field to Account"Treat retrieved metadata like any other code change. Read the diff before you commit. Retrieved files can include unrelated changes, such as reordered XML elements or a profile that picked up extra permissions.
Handling conflicts
A conflict means the same component changed locally and in the org since the last sync. By default, the CLI stops and lists the conflicting components instead of overwriting either side.
You have two ways to resolve one:
- Decide which side wins. To keep your local version, deploy with the ignore-conflicts flag. To keep the org version, retrieve with the same flag.
- Merge by hand. Retrieve the org version into a separate branch, compare it with your local file in Git, combine the changes, and then deploy the result.
sf project deploy start --ignore-conflicts --target-org my-scratch
sf project retrieve start --ignore-conflicts --target-org my-scratchUse the flag only after you have looked at the preview. It tells the CLI to overwrite without checking, so changes on the losing side are lost. If the losing side was in the org, you can recover the change only if you retrieved it earlier.
Metadata types that behave differently
Source tracking works at the level of source components, and not every change in Setup maps cleanly to a file.
- Decomposed metadata. Custom objects are split into files per field, list view, and similar parts. Tracking follows each part, so adding one field shows up as one changed file.
- Settings. Some org settings come from the scratch org definition file and the
settingsmetadata. A change you make in Setup may retrieve as a settings file that you do not want in your project. Check the diff. - Profiles and permission sets. A retrieve may return only part of a profile, depending on which components are also being retrieved. Prefer permission sets for access control because their diffs are smaller and easier to review.
- Deletions. If you delete a component in Setup, the next retrieve removes the local file. If you delete a local file, the next deploy does not remove the component from the org unless you push the deletion. On a tracked org, the CLI detects local deletions and treats them as changes to apply. Confirm in the preview.
- Unsupported types. Some metadata types are not tracked. Changes to them do not appear in the preview, so retrieve them by name with
--metadatawhen you need them.
Where tracking stops working
Source tracking is a convenience, not a guarantee. These situations are common sources of confusion:
- A new clone or a new machine. The tracking record is local. A freshly cloned project against an existing org has no history, so the first deploy sends everything, and the first retrieve brings everything down. Expect a large first sync.
- A refreshed or recreated sandbox. A refresh produces a new copy of the org. Old tracking data no longer matches it. Reset tracking, as shown below, or delete the old org alias and authorize again.
- Hand-edited files outside the package directory. Only files under directories listed in
sfdx-project.jsonare tracked. - Deploys through other tools. If someone deploys to your scratch org with a different tool, the org's revisions advance without your CLI seeing them. The CLI will see them as remote changes at the next status check, which is correct, but it can look like a flood of unexpected changes.
You can reset tracking so the CLI treats the current state as the baseline:
sf project reset tracking --target-org my-scratchThis does not change the org or your files. It only rewrites the tracking record. Afterward, the CLI treats current differences as new, so use it only when you have already reconciled the org and the project. The related clear command deletes the local record altogether, and the next sync rebuilds it.
A typical daily loop
Here is a working cycle for one scratch org and one feature branch:
git checkout -b feature/account-region
sf org create scratch --definition-file config/project-scratch-def.json --alias region-work --duration-days 7 --set-default
sf project deploy start
# Build a custom field in Setup, then:
sf project retrieve preview
sf project retrieve start
git add force-app
git commit -m "Add Region field"
# Edit an Apex class locally, then:
sf project deploy start
sf apex run test --test-level RunLocalTests --wait 10
git commit -am "Use Region in AccountService"The loop follows one rule. Everything that should survive the scratch org must end up in a Git commit. When the org expires, the commits are what remain.
Key points
- Source tracking compares local files and org revisions with the last synced state and classifies each component as local, remote, or conflicting.
- It is on by default for scratch orgs, optional for Developer and Developer Pro sandboxes, and unavailable for Partial Copy, Full, and production.
sf project deploy startpushes,sf project retrieve startpulls, and the preview commands show the effect first.- Conflicts stop the operation. Resolve them deliberately and do not use
--ignore-conflictsas a habit. - The tracking record is local to one clone and one org, so it is lost or stale after a new clone or a sandbox refresh.
Exercise
You enabled source tracking on a Developer sandbox and have been pulling changes for weeks. The sandbox is refreshed over the weekend. On Monday, sf project deploy preview lists dozens of components you did not touch. What most likely happened, and what should you do before deploying?
Show answer
The refresh created a new copy of the org, so the old tracking record no longer matches its revision history, and the CLI reports many differences. Do not deploy yet. Inspect the preview and compare the org with your Git branch. Once the project and org agree, run sf project reset tracking --target-org <alias> to set a new baseline. If tracking was not re-enabled for the refreshed sandbox, enable it in Setup first.
Exercise
A teammate changed a validation rule in a scratch org through Setup. You edited the same rule file locally. When you run sf project deploy start, the CLI stops and lists the rule. Name two ways to proceed, and say which is safer when you do not know what the teammate changed.
Show answer
You can deploy with --ignore-conflicts to keep your local version, or retrieve with --ignore-conflicts to keep the org version. You can also retrieve the org version into a separate branch, merge the two versions in Git, and deploy the merged result. The merge is safer when you do not know what the teammate changed, because neither version is overwritten without review.