Sub topic 12
Source tracking and resolving conflicts
This chapter assumes you can run sf project retrieve start and sf project deploy start against an authorized org, and that you use Git for your project. It covers how the CLI decides what to move when you give it no explicit selection, and what to do when local and org changes collide.
What source tracking is
Source tracking is the CLI's record of which components changed locally and which changed in the org since your last sync. With that record, sf project deploy start and sf project retrieve start can run without --source-dir, --metadata, or --manifest and still know what to move.
Tracking has two halves:
- Remote tracking. The org records each component change (who changed it, and a revision number) in an internal object. You don't query it directly.
- Local tracking. The CLI stores a snapshot of file checksums and the last synced revision numbers in your project, under the
.sfdirectory. This directory is not source. Keep it out of Git.
A "change" is detected by comparing both halves against the last sync point. A component is changed locally if its file checksum differs from the snapshot. It is changed remotely if the org's revision number is higher than the one the CLI last recorded.
Where tracking is available
Tracking works only with orgs that support it:
| Org type | Source tracking |
|---|---|
| Scratch org | On by default |
| Developer and Developer Pro sandbox | Available. On by default for sandboxes created recently, and can be enabled for older ones in Setup |
| Partial Copy and Full sandbox | Not available |
| Production, Developer Edition | Not available |
For orgs without tracking, you must always select components explicitly. The no-flag forms of the commands fail or do nothing useful there.
Previewing before you move anything
The preview commands show what the CLI would do and which components conflict. They change nothing in the org or your project.
sf project deploy preview --target-org my-sandbox
sf project retrieve preview --target-org my-sandboxEach command prints a table. Components fall into these groups:
| Group | Meaning |
|---|---|
| Will deploy / Will retrieve | Changed on one side only. Safe to move. |
| Will delete | Deleted on one side, so the other side's copy would be removed |
| Conflicts | Changed on both sides since the last sync |
| Ignored | Matched by `.forceignore`, so never moved |
Run both previews before a sync. A deploy preview with an empty Conflicts group tells you the deploy won't be blocked by tracking.
A preview is not a dry-run deploy. It compares tracking data and does not contact the metadata API to check whether the deploy would succeed. The dry run from an earlier chapter (--dry-run) does that. Use the preview to decide what moves and the dry run to check whether it works.
The no-flag workflow
In a tracked org, the basic loop is:
# Pull in changes someone made in the org (for example, with Setup clicks)
sf project retrieve start --target-org my-sandbox
# Edit files locally, then push only what changed
sf project deploy start --target-org my-sandboxThe first command retrieves only components changed remotely. The second deploys only components changed locally. After each successful run, the CLI updates both halves of the tracking data so those components stop appearing as changed.
If you pass --source-dir, --metadata, or --manifest, you pick the components yourself and tracking no longer decides the selection. Tracking data is still updated for the components that moved.
What a conflict is
A conflict is one component that changed on both sides since the last sync. Suppose you edit AccountService.cls locally, and a colleague edits the same class in the org through the Developer Console. Neither side can tell which version is correct, so the CLI refuses to pick.
Conflicts are checked in both directions:
- A deploy stops if it would overwrite an org change you haven't retrieved.
- A retrieve stops if it would overwrite a local change you haven't deployed.
The error lists the conflicting components. It appears before anything is written, so a conflict never half-applies.
Resolving a conflict
You have three options. Choose by deciding which version should survive.
Keep the local version
Deploy with --ignore-conflicts (short form -c). The local file overwrites the org's copy, and the org-side edit is lost.
sf project deploy start --target-org my-sandbox --ignore-conflictsKeep the org version
Retrieve with --ignore-conflicts. The org's copy overwrites your local file, and the uncommitted local edit is lost.
sf project retrieve start --target-org my-sandbox --ignore-conflictsKeep both by merging
When both edits matter, let Git do the merge. Commit your local work first so the overwrite is recoverable, then retrieve the org's version on a separate branch and merge.
git add -A
git commit -m "Local changes to AccountService"
git switch -c org-changes
sf project retrieve start --target-org my-sandbox --ignore-conflicts
git add -A
git commit -m "Org changes to AccountService"
git switch -
git merge org-changesGit marks any overlapping lines with conflict markers. Edit the files to the final content, commit, and deploy. Because the org's remote revision is now recorded as retrieved, the merged result deploys as a local-only change.
One detail makes this work. The retrieve in the second step overwrote your files, but you committed them first, and the branch switch restores them. The retrieve also advanced the tracking snapshot, so the CLI no longer sees the org edit as unseen.
Repairing tracking itself
Sometimes the tracking data is wrong, not your files. This happens when you copy a project to a new machine, restore .sf from a backup, or see components that appear changed on every run. Two commands fix this.
sf project reset tracking --target-org my-sandbox
sf project delete tracking --target-org my-sandboxreset trackingrebuilds tracking so that the org and your local files are treated as in sync, with no changes pending. It also accepts--revisionto set the last synced revision explicitly. Use it only after you have confirmed your local files already match the org, or the next deploy will skip real differences.delete trackingremoves local tracking files for the org. Afterward, the CLI has no baseline, so use explicit selection for the next retrieve or deploy.
Common errors
- Tracking commands report that the org doesn't support tracking. You targeted production or a sandbox type without tracking. Add
--source-dir,--metadata, or--manifest. - A deploy reports conflicts for components you didn't touch. Another user, or an installed package upgrade, changed them in the org. Retrieve them, or check them with the retrieve preview.
- Everything shows as changed after cloning the repository. Tracking files live outside Git, so a fresh clone has no baseline for your org. Run
sf project reset trackingafter confirming your files match the org, or retrieve first. - A component you deleted locally is still in the org. Deletion shows as "Will delete" in the preview. Deploying with the no-flag form removes it from tracked orgs. In untracked orgs, use the destructive changes covered later in this tutorial.
- The same component conflicts again after you resolved it. You resolved it in files but never synced. A conflict clears only after a successful deploy or retrieve of that component.
Exercise
You edited InvoiceService.cls locally and left the change uncommitted. A teammate then edited the same class in your tracked sandbox. sf project deploy start --target-org my-sandbox stops with a conflict. You want to keep both sets of changes. List the steps in order.
Show answer
First run sf project deploy preview and sf project retrieve preview to confirm which components conflict. Commit your local change so it is safe. Switch to a new branch and run sf project retrieve start --target-org my-sandbox --ignore-conflicts, then commit the retrieved org version. Switch back to your original branch and run git merge on the new branch, resolve any conflict markers in InvoiceService.cls, and commit. Finally, run sf project deploy start --target-org my-sandbox. The merged file deploys as a local change, and the conflict is gone.