Sub topic 1
What Source Tracking Is and Why It Matters
Prerequisites
This chapter assumes you have the following:
- A Salesforce DX project with
sfdx-project.jsonand aforce-appdirectory. - The Salesforce CLI (
sf) installed and an org authorized. - Familiarity with metadata components such as Apex classes and custom objects.
- Basic Git and command-line usage.
The problem tracking solves
A Salesforce org holds metadata: Apex classes, custom objects, flows, and more. Your local project holds a copy of that metadata as files. Both sides change. You edit a class in VS Code, and a teammate or an admin edits a flow in the org's Setup pages.
Without help, you must remember what changed and where. You would either deploy everything every time, which is slow, or name each changed component by hand, which is error-prone.
Source tracking removes that bookkeeping. The Salesforce CLI records which components changed locally and which changed in the org. It then moves only the differences.
What the CLI records
Tracking compares two things for each component:
- The local state, which is the content of your project files compared with a snapshot the CLI saved earlier.
- The org state, which is a revision number the org assigns to each component whenever it changes.
When the two sides are synchronized, nothing is pending. When you edit a file, the CLI sees it differ from its snapshot and marks it as a local change. When someone edits the component in the org, its revision number increases and the CLI marks it as a remote change.
If the same component changed on both sides, the CLI reports a conflict instead of silently overwriting one version. Later chapters cover conflicts in detail.
Tracking in practice
Not every org supports tracking. Scratch orgs do, so they are the usual place to see it work. You create one like this:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultAfter you edit local files, run the deploy command with no source flags:
sf project deploy start --target-org my-scratchBecause the org is tracked, the CLI deploys only the components you changed locally. The matching command retrieves only what changed in the org:
sf project retrieve start --target-org my-scratchYou can also look before you move anything:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe preview commands list pending changes and conflicts without changing the org or your files.
Why it matters
- Speed. Deploying a handful of files takes far less time than deploying the whole project.
- Safety. Conflict detection stops you from overwriting a teammate's or admin's change without noticing.
- Less manual work. You do not maintain lists of changed components or manifests for everyday development.
- Two-way sync. Changes made through Setup can be pulled into your project, so your files stay accurate.
Common misunderstandings
- Tracking is not version control. It compares your files with one org's state. It keeps no history of your changes, so keep using Git.
- Tracking is per org. Each org you work with has its own tracking state.
- Tracking does not stop at "files I saved." It watches the project's metadata files, except those excluded by
.forceignore.
Exercise
You edit one Apex class locally, and an admin edits a flow in the same tracked scratch org. You run sf project deploy start --target-org my-scratch with no source flags. What does the CLI deploy, and what does it do about the flow?
Show answer
It deploys only the Apex class, because that is the only local change. The flow is a remote change, so it is not deployed. It would be retrieved by sf project retrieve start, or you could see it first with sf project retrieve preview.