Sub topic 3
How the CLI knows what changed
This chapter assumes you can work in a Salesforce DX project (sfdx-project.json, force-app), have the sf CLI installed with an org authorized, and know basic Git. The earlier chapters covered what source tracking is and which orgs support it. This chapter explains how the CLI decides which components changed.
The two questions the CLI must answer
When you run sf project deploy start without naming any source, the CLI has to answer two questions:
- Which components changed on my machine since the last time the project and the org matched?
- Which components changed in the org since that same moment?
Neither question can be answered from the current state alone. The CLI needs a record of the last point where both sides agreed. Source tracking keeps that record in two places, one on each side, and compares each side against it.
| Side | What is stored | Where | What it answers |
|---|---|---|---|
| Org | A revision counter for each changed component | The org itself | What changed remotely? |
| Local | A snapshot of the files at the last sync | The `.sf` directory in your project | What changed locally? |
Org-side tracking: revision numbers
A tracked org, such as a scratch org, records every change to a source-trackable component. Each record is a row in the SourceMember object, which you can query through the Tooling API. A row holds:
MemberTypeandMemberName, for exampleApexClassandMyClass.RevisionCounter, an integer that increases with every change in the org.IsNameObsolete, which is true when the component was deleted or renamed.ChangedBy, the user who made the change.
The counter is global to the org, not per component. If you edit a class and then a custom object, the object gets a higher number than the class. The highest counter the CLI has already seen is the marker for "last sync" on the org side.
You can look at these records yourself:
sf data query --use-tooling-api --target-org my-scratch \
--query "SELECT MemberType, MemberName, RevisionCounter, IsNameObsolete FROM SourceMember ORDER BY RevisionCounter DESC LIMIT 10"The result shows the ten most recent changes. Any row with a counter higher than the CLI's stored maximum counts as a remote change.
Both UI edits and API changes advance the counter. A teammate who edits a Flow in Setup creates a revision, and so does a deploy from another machine. The org does not care how the change arrived.
Local tracking: a snapshot of what you last synced
The org cannot see your files, so the CLI tracks local changes itself. After each successful sync, it stores a record of every tracked file as it was at that moment. The CLI keeps this in a hidden Git-style repository under .sf/orgs/<orgId>/ in your project. Each org gets its own folder, keyed by org ID. The next chapter covers what lives in that folder. For now, it matters that the snapshot is per org and per project directory.
To find local changes, the CLI compares the files in your package directories with the snapshot:
- A file with different content than the snapshot is a modified component.
- A file not in the snapshot is an added component.
- A snapshot entry with no file is a deleted component.
The comparison uses file content, not timestamps. Opening a file and saving it without changes does not make it a local change. Touching a file with touch does not either. A change of a single character does.
The CLI only looks at files it considers source. Files matched by .forceignore are left out of the comparison. This is why .forceignore affects tracking and not only deployment. A later chapter covers it in detail.
How one sync cycle works
The diagram shows what happens when you deploy and then retrieve.
Three details matter here.
First, the CLI checks both sides on every command, not only the side it is about to act on. A deploy also looks at remote changes because it needs them to detect conflicts.
Second, the state only moves forward after a successful operation. If a deploy fails, the snapshot and the stored revision stay where they were. The failed components still count as local changes, and the next deploy tries them again.
Third, your own deploy creates revisions in the org. The CLI knows about them because it reads them back after the deploy and records them as already synced. Without that step, your own deploy would show up as a remote change and the next retrieve would pull back files you just sent.
Seeing the result without changing anything
The preview commands run the same comparisons and print the outcome. They write nothing to the org or to your files.
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe deploy preview lists local changes that would be sent, remote changes that would conflict, and files that would be ignored. The retrieve preview lists the org changes it would pull. Use these first whenever you are unsure what the CLI believes has changed.
Running the commands with no source flags then acts on exactly those lists:
sf project deploy start --target-org my-scratch
sf project retrieve start --target-org my-scratchWhy a change can look bigger than you expect
The org tracks components, not lines or fields. That has some consequences.
- Editing one field on a custom object marks the whole object as changed, because the object's source file holds its definition. If your project stores fields as separate files, the field is the component.
- A deploy that touches a component without changing its content can still advance its revision in the org.
- A component can have several files. An Apex class has a
.clsfile and a-meta.xmlfile, and Lightning web components have a folder of files. A change to any of them marks the whole component. - Metadata types differ in how reliably the org tracks them. Some types are not trackable at all, so they never appear in
SourceMember. For those, you must name them explicitly.
If the preview shows a component you did not touch, the likely cause is formatting. Line endings or whitespace changes made by an editor or by Git's autocrlf setting alter file content, and the content comparison sees that as a change.
Common failure modes
| Symptom | Likely cause |
|---|---|
| Preview shows no local changes after you edited a file | The file path matches a `.forceignore` pattern, or the file is outside a package directory in `sfdx-project.json` |
| Every file appears changed | The `.sf` folder was deleted or the project was cloned fresh, so there is no snapshot to compare against |
| Retrieve returns your own recent deploy | The org-side revision was not recorded after the deploy, often because a deploy was interrupted |
| Tracking commands behave as if there is no tracking | The target org does not have tracking enabled, such as a production org |
The second row deserves emphasis. The snapshot is local and is not part of your Git history, because .sf is excluded by the default .gitignore in a DX project. A teammate who clones the repository starts with no snapshot. Each person's tracking state is separate, even when they target the same org.
When the two histories disagree and the preview output does not make sense, the answer is to reset the tracking state. The CLI can rebuild the revision pointer so that both sides are treated as matching at the current moment:
sf project reset tracking --target-org my-scratchUse this only after you have compared the sides yourself, because the CLI will then treat any differences as already synced and never offer them. A later chapter covers resetting and clearing in detail.
Summary of the model
- The org assigns an increasing revision number to each changed component and exposes it through
SourceMember. - The CLI remembers the highest revision it has synced. Any higher revision is a remote change.
- The CLI keeps a local snapshot of files as they were at the last sync. Any difference in content is a local change.
- Successful deploys and retrieves advance both markers. Failed ones do not.
- A component changed on both sides since the last sync is a conflict, which the next chapters cover.
Exercise
You edit MyClass.cls locally and run sf project deploy preview. It lists no local changes. Give two causes related to how the CLI detects local changes, and say how you would check each.
Show answer
First, the file may match a .forceignore pattern, so the CLI excludes it from the comparison. Check the patterns in .forceignore and see whether the preview output lists the file as ignored. Second, the file may sit outside every package directory listed in sfdx-project.json, so the CLI never scans it. Check the packageDirectories paths against the file location. A third possibility is that the edit did not change content, because the CLI compares content with the snapshot and not timestamps.
Exercise
A teammate edits an Apex class in the org through Setup while you edit the same class locally. Describe which marker on each side makes the CLI see both changes.
Show answer
The org records a new SourceMember revision number for the class, higher than the maximum revision your CLI has stored, so it counts as a remote change. Your local file now differs from the local snapshot taken at the last sync, so it counts as a local change. The same component is changed on both sides since the last sync, which makes it a conflict.