Sub topic 12
Resetting and clearing tracking state
This chapter assumes you can run sf project deploy preview and sf project retrieve preview. It also assumes you know that tracking state is kept in the .sf directory and in the org itself.
Why tracking state goes wrong
Tracking compares two histories: the org's revision list and the local snapshot in .sf. Sometimes those two no longer describe the same thing. Common causes:
- You deleted or restored the
.sfdirectory, for example withgit clean -fdxor by copying the project to a new machine. - You switched to a branch whose files differ widely from what you last deployed.
- Another developer or a Metadata API tool changed a scratch org that you also use.
- Preview output lists components as changed that you know are identical.
The CLI has two commands for these cases. They do different things, so pick the one that matches the problem.
The two commands
| Command | What it changes | Effect on the next preview |
|---|---|---|
| `sf project clear tracking` | Deletes the local tracking files only | Every local and remote component appears changed, and same-named components appear as conflicts |
| `sf project reset tracking` | Resets local tracking and sets the remote revision to the one you choose, or to the latest | The CLI treats the org and the project as synchronized at that revision |
The two commands also behave differently with a revision number. reset tracking can target a specific revision with --revision. If you omit the flag, it uses the latest revision.
# Delete local tracking files. Prompts for confirmation.
sf project clear tracking --target-org my-scratch
# Start from the org's latest revision, skipping the prompt.
sf project reset tracking --target-org my-scratch --no-prompt
# Reset to a specific remote revision.
sf project reset tracking --target-org my-scratch --revision 5Both commands warn that they delete or overwrite existing tracking files. The files are local and cheap to rebuild, but the next deploy or retrieve will behave differently, so treat both commands as deliberate actions.
Choosing between them
Use reset tracking when you know the project and the org match. A typical case is that you just deployed the whole project with --source-dir force-app and want the CLI to stop reporting stale differences.
Use clear tracking when you do not trust the comparison and want the CLI to start over from the metadata itself. Expect a long list of conflicts afterward, because the CLI no longer knows which side is newer.
A safe reset sequence
- Run both previews and save the output so you can compare later.
- Decide which side is correct. If the org holds changes you need, retrieve them first.
- Commit or stash local work in Git so nothing depends on tracking state.
- Run
sf project reset tracking. - Run both previews again. They should now show no changes.
sf project retrieve preview --target-org my-scratch
git add -A && git commit -m "Save work before tracking reset"
sf project reset tracking --target-org my-scratch --no-prompt
sf project deploy preview --target-org my-scratchExercise
You copied your project to a new laptop without the .sf directory. The scratch org still has your latest work, and you are not sure the local files match it. Which command do you run first, and what do you expect from the next preview?
Show answer
Run sf project clear tracking (or let the CLI rebuild the local state), then preview. The CLI no longer knows which side is newer, so every component appears changed and same-named ones appear as conflicts. Review them, retrieve or deploy what you need, and then use sf project reset tracking once you are sure both sides match.