Sub topic 3
How the CLI knows what changed
This chapter assumes you have a Salesforce DX project, an authorized org, and the sf CLI. It also assumes you know what Apex classes and custom objects are as metadata components. The previous chapters covered which orgs support tracking. Here you will see how the CLI works out what to deploy and what to retrieve.
Two histories, one comparison
Source tracking keeps two separate records of change and compares each against a baseline from your last sync.
- Org side: Salesforce records every metadata change made in a tracked org. Each change is stored as a source member record, which holds the component's type and name and a revision number. The revision number is a counter that only increases, so a higher number means a more recent change.
- Local side: The CLI keeps a snapshot of your project files as they were at the last deploy or retrieve. It compares your current files against that snapshot to find edits, additions, and deletions.
Both records are measured against the same baseline: the point where the org and your project were last known to match.
Following one change through
Suppose you create a scratch org and push nothing yet. Then you edit MyClass.cls locally and run:
sf project deploy preview --target-org my-scratchThe CLI compares your files to the snapshot and finds that MyClass.cls differs. It also asks the org for any revisions newer than the last one it recorded. If there are none, the preview lists one local change and no conflicts.
Now deploy:
sf project deploy start --target-org my-scratchSalesforce accepts the component and assigns it a new revision number. The CLI stores that number and updates the snapshot to match your files. The next preview shows nothing to deploy, because the baseline has moved forward.
Retrieving works the same way in reverse. If an admin edits a Flow in the org, the org's revision for that Flow rises above the one you stored. The CLI sees the difference and marks the Flow as a remote change.
sf project retrieve preview --target-org my-scratchAfter a retrieve, the stored revision catches up and the Flow no longer counts as changed.
What counts as a change
- A component counts as changed locally if its file content differs from the snapshot. Touching a file without altering it does not count.
- A component counts as changed in the org if its revision is higher than the stored one.
- A component changed on both sides since the baseline is a conflict. The next chapters cover conflicts in detail.
Why the baseline matters
Everything above depends on the stored baseline being accurate. If the stored revision or the snapshot is lost, deleted, or copied from another machine, the two sides can no longer be compared reliably. The CLI may then report changes that do not exist or miss ones that do. Later chapters explain where this state lives and how to reset it.
Exercise
You edit an Apex class locally and an admin edits the same class in the org. Neither of you has deployed or retrieved since the last sync. Which of the two records show a change for that class, and what does the CLI call the result?
Show answer
Both. The local snapshot comparison shows the file differs from the snapshot, and the org's revision number for the class is higher than the stored one. A component changed on both sides since the baseline is a conflict.