Sub topic 4
Where tracking state lives on your machine
This chapter assumes you can already create a scratch org, deploy to it with sf project deploy start, and use Git on the command line. It covers the files the Salesforce CLI writes locally to remember what has changed. You need this knowledge when tracking misbehaves, when you clone a project onto a new machine, or when you wonder what is safe to delete.
Why state lives on your machine at all
Source tracking compares two things: what is in your local project and what is in the org. The org knows which components changed and in what order, but it does not know what your local files looked like the last time you synchronized. The CLI has to store that part itself.
So tracking is split in two:
- Org side: the org assigns a revision counter to each change to a trackable component.
- Local side: the CLI keeps a record of the revision it last synchronized, and a snapshot of your local files as they were at that point.
Everything the local side needs sits under a hidden .sf directory in your project root. Nothing about tracking is stored in force-app, sfdx-project.json, or the org itself.
The `.sf` directory
When you run a command against an org for the first time, the CLI creates .sf at the project root, next to sfdx-project.json. Its contents vary by CLI version, but the parts that matter for tracking are stable:
| Path | What it holds |
|---|---|
| `.sf/config.json` | Project-level CLI settings, such as the default `target-org`. Not tracking state. |
| `.sf/orgs/<orgId>/` | One folder per org you have tracked from this project. |
| `.sf/orgs/<orgId>/maxRevision.json` | The remote revision numbers the CLI last saw for each component. |
| `.sf/orgs/<orgId>/localSourceTracking/` | A shadow Git repository that snapshots your local files. |
Older projects may use a .sfdx directory instead of .sf. The CLI migrates what it can, but you may see both folders in a project that has been around for a while.
You can look at the layout yourself. This lists the orgs the CLI has tracked here, then shows the contents of one org's folder. Replace <orgId> with a name printed by the first command.
ls -1 .sf/orgs
ls -a .sf/orgs/<orgId>The folder name is the org's ID, not its alias or username. Aliases can change, but the org ID does not, so the state follows the org even if you re-alias it.
Remote state: `maxRevision.json`
The org stamps each change to a trackable component with a revision number. This is the org's way of saying "this component changed at point N." The CLI queries these records and stores what it learned in maxRevision.json.
The file has two jobs:
- It remembers the highest revision the CLI has synchronized, so the next retrieve asks only for changes after that point.
- It keeps a per-component record of the revision last retrieved and the revision last deployed. If the org's current revision for a component is higher than the one recorded, the org-side copy has changed.
This is what sf project retrieve preview uses to list org-side changes. It is also the org-side half of conflict detection, which a later chapter covers.
You should not edit this file by hand. Its format is internal and can change between CLI versions. If it is wrong, the supported fix is sf project reset tracking.
Local state: the shadow repository
Revision numbers cover the org. For your local files, the CLI cannot ask a server what changed. It needs its own copy of what the files looked like at the last synchronization.
It keeps that copy in .sf/orgs/<orgId>/localSourceTracking/, which is a Git repository. When you deploy or retrieve, the CLI commits the state of your tracked source files into it. The next time it needs to know what changed locally, it compares your working files against that last commit. Git's content hashing makes this comparison fast and exact.
Some consequences follow from this design:
- Content decides, not timestamps. If you open a file, save it unchanged, and run a deploy preview, nothing is listed. If you change one character, the file is listed.
- It is separate from your project's Git history. The shadow repository has its own object store and does not touch your project's
.gitdirectory. Your commits, branches, and stashes have no effect on it, and it has none on them. - It is per org. Each org gets its own shadow repository, because "last synchronized" means something different for each org.
- Only source files count. Files matched by
.forceignoreare not tracked. The CLI also looks only at package directories listed insfdx-project.json.
A useful consequence is that switching Git branches changes your local files, and the next preview shows those files as local changes against the shadow snapshot. That can surprise you, and it is why the team-workflow chapter says more about branches.
What both halves do together
Run a preview and the CLI reads both stores. It diffs your files against the shadow repository to find local changes. It compares the org's current revision numbers against maxRevision.json to find remote changes. It then reports the two lists, and any component on both lists is a conflict.
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchAfter a successful deploy or retrieve, the CLI updates both stores so the components involved count as synchronized. This is why running the same command twice in a row does nothing the second time.
Keep `.sf` out of version control
Tracking state describes the relationship between one machine's files and one org. It means nothing to a teammate, and committing it would give them a record that does not match their files. The project template that sf project generate creates includes .sf/ in .gitignore. Check yours:
grep -n "\.sf" .gitignoreIf you started from an older project or a hand-made repository, add the entries yourself:
.sf/
.sfdx/What happens when state is missing
Because the state is local and disposable, losing it is not a disaster. The org keeps your metadata and Git keeps your source. Only the memory of "what was synchronized" is lost. The common cases look like this:
| Situation | Effect |
|---|---|
| Fresh clone of the repository | There is no `.sf` folder. The first command against a tracked org starts with no history, so the CLI cannot tell what is already in sync. |
| You delete `.sf/orgs/<orgId>` | The CLI forgets that org. The next run rebuilds state, and you may see many components listed as changed. |
| You authorize the same org on a second machine | Each machine has its own state and tracks independently. |
| The org expires or is deleted | The folder stays on disk but is never used again. It is safe to remove. |
In each case the repair is the same. Preview first, then decide how to establish a baseline. When you know the local files and the org already match, sf project reset tracking records that fact. A later chapter covers reset and clear in full, including the revision options.
sf project reset tracking --target-org my-scratchReset changes the stored state to say "synchronized now." It does not deploy or retrieve anything. If your files and the org do not actually match, reset hides the difference rather than fixing it, so preview before you run it.
Stale folders and housekeeping
Scratch orgs expire, usually within 30 days, so a project that creates many of them collects one folder per org under .sf/orgs. Each folder is small, but the list gets noisy. Deleting folders for expired orgs is safe. Deleting the folder for an org you still use is also safe, but you pay for it with a rebuilt baseline.
When you are unsure which folder belongs to which org, compare folder names with the IDs reported by the CLI:
sf org list --all
sf org display --target-org my-scratchThe org display output includes the org ID, which matches a folder name under .sf/orgs. The two commands read different folders, so a mismatch only means the org was authorized elsewhere or the folder was never created.
Summary of the model
- Tracking has an org half (revision numbers) and a local half (a snapshot of your files).
- Both live in
.sf/orgs/<orgId>/, one folder per org. maxRevision.jsonrecords remote revisions.localSourceTrackingis a private Git repository that records local content.- The state is machine-local, so keep it out of Git and never copy it between machines.
- Missing or damaged state is recoverable. Preview, then reset when you know the sides match.
Exercise
A teammate clones your repository and runs sf project deploy start --target-org my-scratch against a scratch org that you also use. They see many components listed as changed, even though you have already deployed all of them. Why does this happen, and why should the .sf folder not be committed to fix it?
Show answer
Tracking state is stored per machine under .sf/orgs/<orgId>/, and a fresh clone has no .sf folder. On their machine the CLI has no shadow snapshot and no recorded revisions for that org, so it cannot tell what is already synchronized and treats the components as changed. Committing .sf would not fix it. The shadow repository would describe your files at your last sync, not theirs, which causes false conflicts or missed changes. The right approach is to preview, then run sf project reset tracking only if you have confirmed their files and the org match, or to use a separate scratch org per developer.
Exercise
You run sf project deploy preview after saving a file without editing it, and nothing is listed. Which part of the tracking design explains this?
Show answer
The local half compares file content against the shadow repository, not modification times. A file that is saved without changes has the same content hash as the snapshot, so it is not reported as a local change.