Sub topic 12
Resetting and clearing tracking state
This chapter assumes you can run sf project deploy preview and sf project retrieve preview against a scratch org or sandbox with tracking. It also assumes you know that tracking state lives under the .sf directory and that a conflict means both sides changed the same component. If those are unfamiliar, review the chapters on previewing changes and resolving conflicts first.
Why tracking state sometimes needs repair
Source tracking compares two histories. The org keeps a revision counter for each metadata component. Your machine keeps a local snapshot of the files as they were at the last sync, plus the highest org revision the CLI has seen. A change is "pending" when the org revision or the local file hash differs from what the snapshot recorded.
That design works while the CLI is the only thing moving metadata. Tracking can drift out of step in a few situations:
- You switched Git branches and many files changed at once, so the CLI reports a large set of local changes that you don't want to deploy.
- A teammate or an automated job changed the org outside your CLI session, so the org reports many remote changes you don't want to retrieve.
- You copied or restored a project folder, and the local snapshot no longer matches the org it points to.
- A deploy or retrieve failed partway, and the preview output no longer matches what you know is true.
- You deleted the scratch org or sandbox, and the local tracking files for it remain on disk.
Two commands handle these cases. They do different things, and picking the wrong one is the usual mistake.
Resetting versus clearing
| Command | What it changes | Typical use |
|---|---|---|
| `sf project reset tracking` | Rewrites the tracking state so the org and local project count as synchronized at a chosen revision | The histories disagree, but you want to keep using the same org |
| `sf project delete tracking` | Removes the local tracking files for an org | The org is gone, or you want the CLI to rebuild its state from scratch |
Neither command deploys or retrieves any metadata. Neither changes your source files or the org's metadata. They change only what the CLI believes about what is in sync.
Resetting tracking
Resetting tells the CLI to accept the current state as the baseline. After a reset, the preview commands report no differences, even if the files and the org really do differ.
sf project reset tracking --target-org my-scratchBy default the command resets to the latest revision in the org. The CLI asks you to confirm first, because the operation discards information about pending changes. You can reset to a specific revision number instead:
sf project reset tracking --target-org my-scratch --revision 12With --revision, every org change at or below revision 12 is treated as already synced. Changes with higher revisions still appear as remote changes in the next retrieve preview. This is useful when you know exactly which org changes you have already captured in source control and want to see only the ones after that point.
To skip the confirmation prompt in a script, add --no-prompt:
sf project reset tracking --target-org my-scratch --no-promptWhat a reset does to pending changes
A reset does not move any content. Consider this sequence:
- You edit
MyClass.clslocally and have not deployed it. - You run
sf project reset tracking. - You run
sf project deploy preview.
The preview is now empty, because the CLI considers the current local state to be synchronized. Your edit exists on disk but is no longer flagged for deployment. A deploy with no source flags will not send it.
A safe reset procedure
Use this order when you decide a reset is the right fix:
- Run
sf project deploy previewandsf project retrieve preview. Note which components appear on each side. - Decide what you want to keep. If the org holds changes you need, retrieve them explicitly with
--metadataor--source-dir. If your local files hold changes you need, commit them to Git. - Run
sf project reset tracking. - Run both previews again and confirm they are empty.
- Deploy or retrieve any components you set aside in step 2 by name.
Steps 2 and 5 turn a reset from a blind overwrite into a deliberate decision.
Clearing tracking state
Clearing removes the local tracking files so the CLI has nothing left to compare against.
sf project delete tracking --target-org my-scratchThe command prompts for confirmation, and --no-prompt suppresses the prompt. It deletes only local files in .sf. It does not touch the org, and it does not delete your source.
Clearing has two common uses.
Cleaning up after an org is deleted. When a scratch org expires or you delete it, the tracking files for its org ID stay in .sf/orgs. They do no harm, but they accumulate. Clearing them keeps the directory tidy.
Starting fresh with a live org. If the local snapshot is corrupted or you can't trace why the histories disagree, clearing discards the snapshot. The next command that needs tracking rebuilds state from the org's current revision list. Expect the first preview afterward to be larger than usual. The CLI has no record of what you had already synced, so it may list many components as changed. Review that list before you deploy or retrieve anything.
Choosing between the two
Worked example: switching branches
You work on a feature branch against a scratch org. You switch to a different branch that has a quite different force-app tree. The next preview lists forty components as local changes, because the files on disk no longer match the snapshot.
You do not want to deploy those forty components. The scratch org already holds the feature work, and you only want to inspect the other branch's files.
- Run the previews and note the output.
- Decide that the org content is correct and the local differences come from the branch switch.
- Reset tracking:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratch
sf project reset tracking --target-org my-scratch --no-prompt
sf project deploy preview --target-org my-scratchThe final preview is empty. From here, a change you make to one Apex class shows up as the only pending local change. Switching branches repeatedly makes this a poor workflow, because each switch invalidates the snapshot again. Consider a separate scratch org per long-lived branch instead.
Where the files are
The local state lives under the .sf directory in your project root, in a folder named for the org ID. It holds a record of the highest org revision seen and a local snapshot of file hashes. The exact file names and layout are internal details that can change between CLI versions. Treat them as read-only. Editing or deleting individual files by hand is the unsupported version of what sf project delete tracking does safely.
Add .sf to your .gitignore. The state describes one machine's view of one org, so sharing it through Git gives teammates a snapshot that does not match their own org.
Pitfalls
- Resetting to silence a conflict. A conflict means two sides changed the same component. A reset removes the warning but not the difference, and the next deploy may overwrite someone's work. Use the conflict workflow first: preview, compare, then
--ignore-conflictsonly for the side you chose. - Resetting an org you do not track. Production orgs and most Developer Edition orgs do not support tracking. The commands have nothing to reset there. Use
--source-dir,--metadata, or--manifestinstead. - Assuming a reset syncs content. It changes bookkeeping only. After a reset, local and org content can still differ.
- Clearing and forgetting the large first preview. Read it before you act on it. A full deploy of every listed component is rarely what you want.
- Sharing
.sf. Committing it causes confusing state on other machines.
Exercise
You edited two Apex classes locally and have not deployed them. You then run sf project reset tracking --target-org my-scratch --no-prompt. What does sf project deploy preview show, and how do you get the two classes into the org?
Show answer
The preview shows no pending changes, because the reset marks the current local state as synchronized. The edits are still on disk, but the CLI no longer flags them. Deploy them by naming them explicitly, for example sf project deploy start --target-org my-scratch --metadata ApexClass:ClassOne --metadata ApexClass:ClassTwo, or with --source-dir force-app/main/default/classes.
Exercise
Your scratch org expired. Which command removes the leftover local tracking files, and does it change any file under force-app?
Show answer
sf project delete tracking --target-org my-scratch removes the local tracking files for that org. It does not change anything under force-app or in any org.