Sub topic 6
Pushing, pulling, and source tracking
This chapter assumes you can run sf commands, keep a Salesforce DX project in Git, and have created a scratch org as shown earlier. It explains how the CLI knows what to send to an org and what to bring back.
What source tracking is
Source tracking is a feature that records the changes made in an org and the changes made in your local project. The CLI compares the two sides and works out what is new, changed, or deleted.
Tracking is on by default in scratch orgs. It is also available in Developer and Developer Pro sandboxes. Partial Copy and Full sandboxes do not support it, so with those you deploy and retrieve by naming the components yourself.
| Org type | Source tracking |
|---|---|
| Scratch org | On by default |
| Developer, Developer Pro sandbox | Available, enabled for the sandbox |
| Partial Copy, Full sandbox | Not supported |
| Production | Not supported |
The push and pull loop
With tracking, two commands cover most daily work. Run them from the project folder with a default org set.
# See what would be sent to the org, without sending it
sf project deploy preview
# Send only the local changes the org does not have yet
sf project deploy start
# See what changed in the org since the last sync
sf project retrieve preview
# Bring those org changes into your local files
sf project retrieve startHere is how a typical cycle works:
- You edit an Apex class locally and run
sf project deploy start. Only that class is deployed. - A colleague, or you in Setup, adds a custom field in the org.
sf project retrieve previewlists it. - You run
sf project retrieve start. The field's metadata lands in your project, and you commit it to Git.
The diagram below shows the same loop.
Git is your source of truth. The org is a working copy, so retrieve org-side changes and commit them before the scratch org expires.
Conflicts
A conflict happens when the same component changed both locally and in the org since the last sync. The CLI stops and lists the conflicting components. This protects you from overwriting a change you did not know about.
You have three options:
- Run
retrieve startfirst, merge the result with your local edits, then deploy. - Add
--ignore-conflictstosf project deploy startto let your local version win. - Add
--ignore-conflictstosf project retrieve startto let the org version win.
sf project deploy start --ignore-conflictsUse these flags only after you have looked at the preview output. They discard the other side's change without a merge.
Working without tracking
In an untracked org, such as a Partial Copy sandbox, the CLI cannot tell what changed. You must say what to move:
sf project deploy start --source-dir force-app/main/default/classes --target-org dev1
sf project retrieve start --metadata ApexClass:PrepareSandbox --target-org dev1You can also pass --manifest package.xml to list components in a file. Selecting components by hand is more error-prone, which is one reason to prefer tracked orgs for daily development.
Resetting tracking
Tracking data is stored locally in your project, under .sf, and in the org. If the two fall out of step, for example after you copy a project to a new machine, the CLI may report odd conflicts. Two commands help:
# Rebuild tracking from the org's current state, keeping local files
sf project reset tracking --target-org my-scratch
# Delete local tracking files, so the next run starts fresh
sf project delete tracking --target-org my-scratchreset tracking can mark the org and your files as in sync even when they differ. Check with deploy preview before you use it.
Common errors
- Not every metadata type is tracked. If a change does not appear in a preview, retrieve it by name.
- Org changes you never retrieved are lost. A scratch org deleted at the end of its lifespan takes untracked Setup work with it.
- Deleted files are not deleted in the org by default. For a tracked org, deletions are detected, but for an untracked one you need a destructive changes manifest.
Exercise
You changed a validation rule in Setup in your scratch org, and also edited the same rule in your local project. sf project deploy start stops with a conflict. What is a safe way to proceed?
Show answer
Run sf project retrieve preview to see the org's version, then run sf project retrieve start to bring the org's change into your project. Merge it with your local edit, commit, and run sf project deploy start again. Use --ignore-conflicts only if you have decided that one side should win.