Sub topic 13
Deploying and retrieving without tracking
This chapter assumes you know the layout of a Salesforce DX project (sfdx-project.json, force-app), have the sf CLI installed with an org authorized, and can use basic Git. It also assumes you have met the idea of source tracking from earlier chapters: the CLI records which components changed locally and in the org so it can move only the differences.
Why some orgs have no tracking
Source tracking is available for scratch orgs and for sandboxes where tracking has been enabled. It is not available for production orgs, for sandboxes without tracking enabled, or for Developer Edition orgs. In an untracked org, the CLI keeps no record of revisions, so it cannot work out what changed. If you run sf project deploy start with no source flags against such an org, the command has nothing to compare and fails with an error asking you to specify what to deploy.
The consequence is simple. In an untracked org, you tell the CLI what to move. The CLI does not decide for you.
The three ways to select source
You choose one or more of these selectors on every deploy or retrieve to an untracked org.
| Flag | You name | Best for |
|---|---|---|
| `--source-dir` | A file or folder path | Moving everything in one part of the project |
| `--metadata` | Metadata type and name, such as `ApexClass:MyClass` | A few known components |
| `--manifest` | A `package.xml` file | Repeatable, reviewable releases |
sf project deploy start --target-org my-sandbox --source-dir force-app/main/default/classes
sf project deploy start --target-org my-sandbox --metadata ApexClass:MyClass
sf project deploy start --target-org my-sandbox --manifest manifest/package.xmlYou can repeat a flag or combine several. For example, --source-dir can be given more than once, and --metadata accepts several values.
Selecting by directory
--source-dir is the most direct selector. Give it a folder and the CLI deploys every component found under it. You can also pass a single file. For a custom object, passing one field file deploys only that field, while passing the object folder deploys the object and all its fields.
Directory selection is easy but coarse. If you deploy force-app as a whole, you send every component in the project, whether you changed it or not. That is slower, and it can overwrite changes that someone else made directly in the org.
Selecting by metadata name
--metadata takes a type and a name separated by a colon. The type is the metadata API name, such as ApexClass, CustomObject, or LightningComponentBundle. A trailing wildcard selects by name prefix, for example ApexClass:Billing*. A type with no name, such as --metadata ApexClass, selects every component of that type in your project.
This selector is useful when you know exactly which components you touched. The CLI looks the names up in your local project and finds the matching files, so the components must exist under a package directory listed in sfdx-project.json.
Selecting by manifest
A manifest is a package.xml file that lists the components to move. It is the same format the Metadata API uses. This is the right choice when a release needs a fixed, reviewable list, because you can commit the file and check it in a pull request.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>MyClass</members>
<members>MyClassTest</members>
<name>ApexClass</name>
</types>
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
<version>62.0</version>
</Package>The version must be an API version your org supports. Use a value that matches sourceApiVersion in sfdx-project.json unless you have a reason to differ.
You can generate a first draft from your source instead of writing it by hand:
sf project generate manifest --source-dir force-app/main/default/classes --name manifest/package.xmlReview the result before you use it. A generated manifest reflects what was on disk at that moment, not what you intended to release.
Retrieving without tracking
Retrieval uses the same three selectors. Since the CLI cannot tell what changed in the org, a retrieve brings down exactly what you name, and it overwrites the matching local files.
sf project retrieve start --target-org my-sandbox --metadata ApexClass:MyClass
sf project retrieve start --target-org my-sandbox --manifest manifest/package.xmlBecause overwriting is silent, run git status and git diff afterwards. Git becomes your change detector. Retrieve into a clean working tree, then read the diff to see what the org actually changed.
A wildcard manifest entry such as <members>*</members> retrieves every component of a type. That can be a fast way to capture the state of an org, but it also pulls in components you did not intend to manage, such as managed package content or standard objects with many generated fields.
Previewing without tracking
The preview commands from the earlier chapter rely on tracking data, so they report nothing useful for an untracked org. Use these substitutes instead:
- Run a validation-only deploy with
--dry-run. The org checks the deploy and runs the tests you request, and nothing is saved. - Retrieve into a clean Git working tree, then inspect the diff.
- Review the manifest or the directory list before you run the command.
sf project deploy start --target-org my-sandbox --manifest manifest/package.xml --dry-runA dry run still takes time, because the org processes the whole payload. It is the closest equivalent to a preview in an untracked org.
Controlling what is included
.forceignore still applies in an untracked org. The CLI skips any file matching a pattern in it, whichever selector you use. Use it to keep editor configuration and test scaffolding out of your deploys.
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**If a component you named does not deploy, check .forceignore before anything else. A name matched by an ignore pattern is dropped without an error in some cases, and the deploy then reports that nothing matched.
Tests and production
Deploys to production have extra rules. By default, a production deploy that includes Apex runs the local tests in the org. You can set the level with --test-level, for example RunLocalTests or RunSpecifiedTests with --tests.
sf project deploy start --target-org prod --manifest manifest/package.xml --test-level RunLocalTestsBecause production has no tracking, a manifest plus a validated dry run is the usual pattern.
Deletes and renames
Without tracking, the CLI cannot see that you deleted a local file, so a normal deploy never removes the component from the org. A rename looks like a new component, and the old name stays in the org.
To delete components, use a destructive changes manifest. Pass it with --post-destructive-changes (or --pre-destructive-changes), together with a regular manifest.
sf project deploy start --target-org my-sandbox --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChanges.xmlThe destructive file has the same shape as package.xml and lists the components to remove. Check it carefully, because a delete in production cannot be undone from the CLI.
Common problems
- "Nothing to deploy" or a no-flags error. You ran a tracking-style command against an untracked org. Add
--source-dir,--metadata, or--manifest. - A component was not deployed. It may sit outside every package directory in
sfdx-project.json, or.forceignoremay match it. - A retrieve changed more files than expected. A wildcard or a broad directory selected extra components. Narrow the selector and read the Git diff.
- A deploy overwrote a teammate's org change. Nothing detects conflicts here. Retrieve first, commit, then deploy.
Summary of the workflow
- Retrieve the components you will touch into a clean Git working tree.
- Commit that state so Git has a baseline.
- Make your changes locally.
- Run a dry-run deploy with a manifest or an explicit list.
- Deploy for real, and commit the manifest alongside the code.
Exercise
You have a sandbox without source tracking. You edited the Apex class InvoiceService and deleted the class OldInvoiceHelper locally. You run sf project deploy start --target-org my-sandbox --metadata ApexClass:InvoiceService. What happens to each class in the org, and what must you add to remove OldInvoiceHelper?
Show answer
InvoiceService is deployed with your local changes. OldInvoiceHelper stays in the org, because without tracking the CLI does not notice the local delete and a normal deploy never removes components. To remove it, create a destructiveChanges.xml that lists ApexClass with the member OldInvoiceHelper, and pass it with --post-destructive-changes alongside a regular manifest.
Exercise
You retrieve with --manifest manifest/package.xml into a working tree that already has uncommitted edits. Why is this risky, and what should you do first?
Show answer
The retrieve overwrites matching local files without warning, so your uncommitted edits can be lost or mixed with the org's version. Commit or stash your work first, so that git diff after the retrieve shows only what the org changed.