Sub topic 12
Source Tracking and Resolving Conflicts
This chapter assumes you can authorize an org, run sf project retrieve start and sf project deploy start, and use a DX project with a .forceignore file. It covers how the CLI tracks changes and what to do when local and remote changes collide.
What source tracking does
Source tracking compares your local project with an org and records which components changed on each side. It works only with orgs that have tracking enabled. Scratch orgs and Developer and Developer Pro sandboxes have it on by default. Production and other sandbox types do not support it.
When tracking is on, the CLI keeps a local record of the org's state. You can then run commands without naming components:
sf project deploy startwith no source selector deploys only the local changes.sf project retrieve startwith no source selector retrieves only the remote changes.
Previewing before you move anything
The preview commands show what each direction would do. They don't change the org or your files.
sf project deploy preview --target-org my-sandbox
sf project retrieve preview --target-org my-sandboxThe output lists components in these groups:
| Group | Meaning |
|---|---|
| To deploy or retrieve | A change exists on one side only |
| To delete | The component was removed on one side |
| Conflicts | Both sides changed the same component |
| Ignored | `.forceignore` excludes the component |
How conflicts arise
A conflict occurs when a component changed both locally and in the org since the last sync. A common case is an admin editing a page layout in a sandbox while you edit the same layout in your branch.
By default, both commands stop when they find a conflict. This protects you from overwriting work you haven't seen.
Resolving a conflict
First, decide which version you want. Run the preview commands to see which components conflict. If you need both sets of changes, retrieve to a separate branch or directory and merge by hand, for example with Git.
Then use --ignore-conflicts to say that one side wins:
# Local files overwrite the org
sf project deploy start --ignore-conflicts --target-org my-sandbox
# Org versions overwrite local files
sf project retrieve start --ignore-conflicts --target-org my-sandboxWhen tracking is out of sync
Sometimes the local tracking record no longer matches reality. This can happen if you copy a project to a new machine or delete the tracking files. You can reset it:
sf project reset tracking --target-org my-sandboxThis command rebuilds the record from the current org state. Local changes made before the reset may then no longer appear as pending, so review them with Git first. To only pull the org's state into the record, use sf project delete tracking and then retrieve.
Common errors
- Nothing to deploy or retrieve. Tracking found no changes. Confirm you saved your files and that
.forceignoreisn't excluding them. - Tracking isn't available. The org is a production org or a sandbox type without tracking. Use
--source-dir,--metadata, or--manifestinstead. - Conflict reported for a file you didn't touch. Another user or an automated process changed it in the org. Preview, inspect the difference, then choose a winner.
Exercise
You run sf project deploy start with no selector against a Developer sandbox, and the command stops with a conflict on Account-Account Layout.layout-meta.xml. You need to keep the admin's changes in the org. What do you do?
Show answer
Retrieve the org's version with sf project retrieve start --ignore-conflicts --target-org my-sandbox, so the org's layout replaces your local file. Commit your local edits to Git first so nothing is lost, then re-apply any of your own edits and deploy again.