Sub topic 1
What source tracking is and why it matters
Source tracking is a feature of the Salesforce CLI (sf). It records which metadata components changed in your local project and which changed in an org. With that record, the CLI can move only the differences instead of everything. This chapter explains the problem it solves, the basic model, and how it changes the commands you run every day. Later chapters cover the mechanics, conflicts, and troubleshooting in detail.
Prerequisites
This chapter assumes you already have the following:
- A Salesforce DX project with an
sfdx-project.jsonfile and a package directory such asforce-app. - The Salesforce CLI (
sf) installed, and at least one org authorized. - Familiarity with metadata components such as Apex classes, custom objects, and Lightning web components.
- Basic Git and command-line skills.
You don't need to know how the CLI stores tracking data. That comes later.
The problem: moving too much, or too little
A Salesforce DX project can hold hundreds or thousands of metadata files. Suppose you edit one Apex class. Without tracking, you have two choices:
- Deploy the whole project. This is slow and can overwrite org changes you didn't know about.
- Deploy by hand-picking the changed file, which means you must remember exactly what you touched.
The same problem applies in reverse. If an admin adds a custom field in Setup, you must know which component to retrieve. In a team, nobody remembers every edit.
Source tracking removes that bookkeeping. The CLI keeps a record of changes on both sides and compares them for you.
What source tracking records
Tracking follows two kinds of change:
- Local changes: files in your project that you added, edited, or deleted since the last time the project and the org were in sync.
- Remote changes: components that were created, changed, or deleted in the org, for example through Setup, another developer's deploy, or the Developer Console.
The CLI compares the two sets. This gives you three useful results:
- Deploy only what you changed locally.
- Retrieve only what changed in the org.
- Detect a conflict: the same component changed on both sides, so moving it in either direction would overwrite someone's work.
The diagram shows the idea.
Which orgs support it
Not every org can be tracked. Tracking depends on the org keeping its own record of component changes, and only some org types do that.
| Org type | Source tracking |
|---|---|
| Scratch org | Supported by default |
| Developer and Developer Pro sandbox | Supported when tracking is enabled for the sandbox |
| Production, Developer Edition, and other sandbox types | Not supported. You choose what to move explicitly. |
Check the current Salesforce documentation for the exact sandbox rules, because they have changed across releases. The next chapter covers this in detail. For now, remember the practical effect: the same commands behave differently depending on whether the target org is tracked.
A first look at tracking in practice
A scratch org is the easiest place to see tracking at work. Create one from your project's definition file and make it your default:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultBecause this org supports tracking, the CLI starts recording changes as soon as it exists. Before you move anything, you can ask what would happen:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe first command lists local changes that a deploy would send, and also any conflicts. The second lists org changes that a retrieve would bring down. Neither command changes anything.
Now run the real commands with no source flags:
sf project deploy start --target-org my-scratch
sf project retrieve start --target-org my-scratchIn a tracked org, deploy start with no --source-dir, --metadata, or --manifest flag deploys only your local changes. retrieve start with no flags retrieves only the changes made in the org. This is the central benefit: you describe the direction, and the CLI works out the contents.
A simple model of how it works
You don't need the internals yet, but a working model helps you predict behavior.
- The org keeps a list of components with a revision number that increases when a component changes.
- Your machine keeps a local snapshot of the state that was last synchronized. It lives inside your project in a hidden
.sfdirectory. - When you run a preview, deploy, or retrieve, the CLI compares your files with the snapshot to find local changes. It compares the org's revision numbers with the snapshot to find remote changes.
- After a successful deploy or retrieve, the CLI updates the snapshot, so the moved components no longer show up as changes.
Two consequences follow from this model:
- The tracking state is tied to one org and one project folder on one machine. A fresh clone of your repository has no snapshot for your scratch org, and another developer's machine has its own.
- The snapshot is a cache of what the CLI believes is synchronized. If it doesn't match reality, you get wrong results. For example, you may see components listed as changed that aren't, or you may see conflicts that you don't expect. A later chapter covers resetting this state.
Why it matters for your workflow
Tracking changes how you work in four ways.
Speed. Deploying five changed files is quicker than deploying a whole project, especially when the project has many Apex classes that would also trigger test runs and compilation.
Safety. Conflict detection protects work. If a colleague edited a page layout in the org and you edited the same layout locally, the CLI stops and tells you instead of silently overwriting one version.
Declarative changes become easy to keep. Developers often configure things in Setup, such as a custom field or a permission set, because the UI is faster. With tracking, sf project retrieve start pulls exactly those edits into your project, where you can commit them to version control.
Less manual scoping. You don't need to maintain a manifest (package.xml) for every small change.
Limits and common misunderstandings
Source tracking is useful, but it has boundaries.
- It is not version control. It tracks differences between your folder and one org, not history. Use Git for history, review, and collaboration.
- It does not guarantee a correct deployment. A tracked deploy can still fail validation, test requirements, or dependency checks.
- It does not cover every file in the project. Files listed in
.forceignoreare skipped when deploying and retrieving, so they are not synchronized with the org. For example:
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**- It does not replace explicit deploys to orgs without tracking. Production deploys, for example, usually use a manifest or a package, and that is intentional.
- Not every Setup change appears as a retrievable change in every case. If something doesn't show up, check whether that metadata type is supported by the metadata API and by tracking.
What you can do after this chapter
You should be able to:
- Say what tracking records and why that allows partial deploys and retrieves.
- Predict whether an org supports it by its type.
- Recognize that a no-flag
deploy startorretrieve startmeans "only the changes" in a tracked org. - Explain that tracking is a local, per-org, per-machine cache, not a replacement for Git.
The next chapter looks at the tracked and untracked org types more closely.
Exercise
You run sf project deploy start --target-org my-scratch with no other flags. Your colleague has changed a custom object in the org, and you changed the same custom object locally. What should you expect from the CLI, and what should you do before forcing the deploy?
Show answer
Because my-scratch is a tracked org, the command tries to deploy only your local changes. The CLI should detect that the same component changed on both sides and report a conflict instead of overwriting the org's version. Before using --ignore-conflicts, run sf project deploy preview --target-org my-scratch and sf project retrieve preview --target-org my-scratch to see both sets of changes, then decide which version to keep, or retrieve the org version and merge it locally.
Exercise
A teammate asks why sf project deploy start --target-org my-production with no flags doesn't deploy just their changes the way it does for their scratch org. What is the reason, and which flags can they use instead?
Show answer
Production doesn't support source tracking, so the CLI has no record of changes and can't compute them. They must choose the content explicitly with --source-dir, --metadata, or --manifest.