Sub topic 6
Previewing changes before you move them
This chapter assumes you have a Salesforce DX project with a force-app directory, the sf CLI installed, and an authorized scratch org or sandbox with source tracking enabled. It also assumes you know basic Git and command-line usage. The goal is to use the preview commands to see what a deploy or retrieve would do before you run it.
Why preview first
Source tracking lets the CLI compute which components changed locally and which changed in the org. A preview shows you that computation without acting on it. Nothing is deployed, retrieved, or written to disk.
Preview matters for three reasons:
- You see the exact set of components that would move, so an unexpected file stands out before it reaches the org.
- You see conflicts before they block you. A conflict is a component changed on both sides since the last sync.
- You can decide whether a force option such as
--ignore-conflictsis justified, instead of using it by reflex.
A preview needs an org that supports tracking, such as a scratch org or a tracked sandbox. In an org without tracking, the CLI has no change history to compare against. Chapter material on untracked orgs covers the alternatives.
The two preview commands
There is one preview command for each direction.
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe first command answers "what would I push to the org?" The second answers "what would I pull from the org?" Both accept --target-org (alias -o). If you set a default org with --set-default when you created it, you can leave the flag out.
Each command prints tables. The deploy preview lists the changes it would send. The retrieve preview lists the changes it would bring down. Either one can also list conflicts.
How to read the output
The output groups components by what would happen to them. The exact column layout can vary between CLI versions, but the information is consistent.
| Group | Meaning |
|---|---|
| To deploy (or to retrieve) | Components changed on the source side that would move |
| To delete | Components removed on the source side that would be deleted on the other side |
| Conflicts | Components changed on both sides, which block a normal deploy or retrieve |
| Ignored | Components matched by `.forceignore` that the CLI skips |
For each row you typically see the component type, the component name, and the file path. Use this to check three things:
- Every component you edited appears in the deploy preview.
- No component appears that you did not touch. If one does, a tool, formatter, or script may have rewritten the file.
- The conflict list is empty, or you understand every entry in it.
A worked example
Start with a scratch org that has been created and has a default alias.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultPush your project and then edit one Apex class locally, for example force-app/main/default/classes/AccountService.cls. Run the deploy preview:
sf project deploy preview --target-org my-scratchYou expect one row: the ApexClass named AccountService, plus its -meta.xml file. If you also see unrelated components, check whether your editor reformatted them. Run git diff to confirm which files differ on disk.
Now, in the org, change a different Apex class through the Developer Console or Setup. Run the retrieve preview:
sf project retrieve preview --target-org my-scratchYou expect the class you changed in the org to be listed as a change to retrieve. The class you edited locally does not appear in the retrieve list, because it has no org-side change.
Both previews together give you a complete picture of the divergence between the project and the org.
What a preview does and does not compare
A preview compares against tracking state, not against the full contents of the org. This has consequences.
- It reports components that tracking knows changed. It does not scan every component in the org and diff it against your files.
- A component that was never tracked, such as a type the org does not report changes for, can be absent from the output even though it differs.
- If tracking state is stale or was reset, the preview reflects that state. A wrong baseline gives a wrong preview.
Conflicts in the preview
A conflict arises when the same component changed locally and in the org after the last synchronization. The preview lists these so you can act before a real deploy fails.
Consider this sequence:
- You sync the project and the org.
- A teammate edits
AccountServicein the org through Setup. - You edit
AccountService.clslocally. - You run
sf project deploy preview.
The preview shows AccountService as a conflict. A normal sf project deploy start would refuse to proceed for that component, which protects the org change from being overwritten silently.
The same applies in reverse. If you run sf project retrieve preview, the same component appears as a conflict, because a retrieve would overwrite your local edit.
The diagram shows how a preview decision flows.
From preview to action
When the preview looks right, run the matching start command. In a tracked org, a start command with no source flags uses the same change computation as the preview.
sf project deploy start --target-org my-scratch
sf project retrieve start --target-org my-scratchThis pairing is useful. What you reviewed is what moves, as long as nothing changed between the two commands. If a teammate edits the org between your preview and your start command, the start command sees the newer state. For a shared org, run the preview and the start command close together.
Forcing past conflicts
If you have reviewed a conflict and know which side should win, override conflict detection:
sf project deploy start --target-org my-scratch --ignore-conflicts
sf project retrieve start --target-org my-scratch --ignore-conflictsThe first makes the local version overwrite the org version. The second makes the org version overwrite your local files. Neither can be undone by the CLI. Commit local work to Git before you retrieve with --ignore-conflicts, so you can recover the files.
Preview and .forceignore
Files matched by .forceignore are excluded from tracking and from deploy and retrieve. A preview is a good way to test your patterns. If a file you want to move does not appear, a pattern may be hiding it. If a file you want to hide appears, the pattern is wrong or misplaced.
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**After you edit .forceignore, run the preview again and compare. Patterns follow gitignore-style matching, and the file sits in the project root.
Common errors and fixes
| Symptom | Likely cause | What to do |
|---|---|---|
| The command reports that the org does not support tracking | The target is a production org or an untracked sandbox | Use `--source-dir`, `--metadata`, or `--manifest` with the start command instead |
| The preview lists many unrelated components | A formatter touched files, or tracking baseline is stale | Check `git diff`, then consider resetting tracking |
| The preview is empty after you edited a file | The file is matched by `.forceignore`, or the edit was in an untracked location | Check ignore patterns and the package directories in `sfdx-project.json` |
| A conflict appears for a component you did not change | The org changed it, or tracking history diverged | Compare versions; reset tracking only if histories are confirmed out of sync |
| The result differs from the earlier preview | The org or your files changed in between | Run the preview again right before the start command |
Resetting when previews look wrong
If the previews keep showing changes that do not exist, the tracking state may no longer match reality. You can reset it.
sf project reset tracking --target-org my-scratchThis tells the CLI to treat the org and the local project as synchronized at a chosen revision. It does not move any files. After a reset, a preview shows only changes made from that point on, so differences that existed before the reset are no longer reported. Use it when you are sure the two sides match, or when you accept that they do not and want a clean baseline.
A habit to build
A reliable routine is:
- Run
git statusto see what is uncommitted. - Run both previews.
- Read the conflict list.
- Run the start command.
- Run the preview again to confirm it is empty.
The last step is a check. An empty preview after a successful deploy shows tracking recorded the change.
Exercise
You edited AccountService.cls locally. A teammate also edited AccountService in the shared scratch org. You run sf project deploy preview. What do you expect to see, what would a plain sf project deploy start do, and what should you do before using --ignore-conflicts?
Show answer
The preview lists AccountService as a conflict, because it changed on both sides since the last sync. A plain sf project deploy start refuses to deploy that component, so the teammate's org change is not overwritten. Before using --ignore-conflicts, run sf project retrieve preview and compare the org version with your local file, for example by retrieving into a Git branch or reading the org copy. Decide which version is correct or merge them by hand, commit your local work to Git, and only then deploy with the flag if your version should win.
Exercise
A deploy preview is empty even though you just edited an Apex class. Name two causes you would check.
Show answer
First, check whether .forceignore has a pattern that matches the file. Second, check that the file is inside a package directory listed in sfdx-project.json. Other valid causes are a target org without tracking, or a tracking state that was reset after your edit.