Sub topic 8
Retrieving changes made in the org
This chapter assumes you have a Salesforce DX project, an authorized scratch org or tracked sandbox, and the sf CLI installed. It also assumes you know what a metadata component is. Earlier chapters explained how tracking works and how to preview and deploy. Here you will pull changes made in the org back into your local project.
Why retrieve at all
Not every change starts in your editor. Admins build flows in Flow Builder, teammates add fields in Setup, and some features can only be configured through the UI. Those edits exist only in the org. Until you retrieve them, your local project is out of date. The next deploy of a related component could also run into a conflict.
In an org with source tracking, the CLI records a revision number for each component on the org side. When you retrieve, it compares those revisions with the ones it has stored locally. It then fetches only the components that changed in the org since the last sync.
Preview first
Run the preview before you retrieve. It changes nothing, in the org or on disk.
sf project retrieve preview --target-org my-scratchThe output groups components by what would happen to them:
- Components that would be retrieved, including new and changed ones.
- Components that would be deleted locally because they were deleted in the org.
- Conflicts, where the same component also changed locally.
- Components that match a
.forceignorepattern and will be skipped.
Treat the conflict group as a stop sign. Read it before you run the real retrieve.
Retrieve only what changed
In a tracked org, retrieve start with no source flags means "retrieve whatever changed in the org".
sf project retrieve start --target-org my-scratchThe command is asynchronous on the server. It requests a retrieve, waits for the result, and writes the returned files into your package directories. For example, if a teammate added a custom field Priority__c to Case in Setup, the CLI writes Case.Priority__c.field-meta.xml under the matching object folder. It also updates the tracking snapshot for that component. A second retrieve right after reports no changes.
If you set a default org with --set-default, you can leave out --target-org. This tutorial keeps the flag so each command is explicit.
What happens to the tracking state
Retrieving only advances tracking for the components it actually fetched. This matters in two cases:
- Components you did not retrieve stay marked as changed in the org. They show up again in the next preview.
- Components that match
.forceignoreare never fetched, so the CLI does not treat them as synchronized.
Retrieving by selection
You can still tell the CLI exactly what to retrieve. The source flags work in a tracked org too.
sf project retrieve start --target-org my-scratch --metadata ApexClass:MyClass
sf project retrieve start --target-org my-scratch --source-dir force-app/main/default/objects
sf project retrieve start --target-org my-scratch --manifest manifest/package.xmlAn explicit selection replaces "only what changed" as the rule. The named components are retrieved even if nothing changed in the org. This is the same approach you use in an untracked org, where the CLI cannot compute differences. A later chapter covers that workflow.
| Command form | Which components are retrieved | Needs tracking |
|---|---|---|
| No source flags | Only components changed in the org since the last sync | Yes |
| `--metadata` | The named components | No |
| `--source-dir` | Components found in the given local directories | No |
| `--manifest` | Components listed in the manifest | No |
A selection that is narrower than the pending changes leaves the other changes pending. If three components changed in the org and you retrieve one by name, the other two still appear in the next preview.
Conflicts during a retrieve
A retrieve conflict occurs when a component changed in the org and also changed in your local files since the last sync. A retrieve would overwrite your local edits, so the CLI stops by default and lists the conflicting components.
The next chapter covers how conflicts arise and how to resolve them. For now, remember the rule: look at the preview, then decide which side wins.
If you have reviewed the conflicts and want the org version to win, override the check:
sf project retrieve start --target-org my-scratch --ignore-conflictsYour local edits to those components are overwritten. If the project is under Git and you have committed your work first, you can recover them. Otherwise they are gone.
Remote deletes
If someone deletes a component in the org, the next preview lists it as a pending delete. Retrieving applies the delete to your local project, so the local file is removed. Check this section of the preview carefully. If a component disappeared because of a mistake in the org, deploy it back instead of retrieving the deletion.
Retrieval and `.forceignore`
.forceignore filters retrieves as well as deploys. A pattern like this keeps matching files out of both directions:
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**This is the usual fix when a retrieve keeps producing noise. Common sources are generated files, or settings and profile components that change whenever anyone touches the org. Add a pattern, and the component drops out of the preview.
Ignoring has a cost. Ignored components are invisible to tracking, so you will not see org-side changes to them. Only ignore what you are sure you do not want in the project.
A typical retrieve cycle
A reliable routine looks like this:
- Commit or stash your local work so the working tree is clean.
- Run
sf project retrieve previewand read all four groups. - If there are no conflicts, run
sf project retrieve start. - Run
git statusandgit diffto review what the retrieve wrote. - Commit the retrieved changes on their own, with a message such as "Retrieve flow changes from scratch org".
Step 4 matters because a retrieve can change the formatting of files you did not touch, for example reordered XML elements. The diff tells you whether those are real changes.
sf project retrieve preview --target-org my-scratch
sf project retrieve start --target-org my-scratch
git status
git diff --statCommon errors
- "Nothing to retrieve" when you know the org changed. The component may match
.forceignore. It may also be a type that is not covered by your package directories, or the change may be in an untracked org. Check the org type first, then the ignore file. - A conflict you did not expect. You or a script may have touched the local file, even only by reformatting it. Compare the local file with the org version before deciding who wins.
- Retrieved files land in an unexpected directory. The CLI places a component in the package directory where it already exists. A new component goes to the default package directory in
sfdx-project.json. Check that setting if new files appear in the wrong package. - The same component shows up on every retrieve. The retrieve did not complete for that component, or tracking state is stale. The reset chapter covers recovery.
Exercise
You run sf project retrieve preview in a scratch org. It lists one custom field under the retrieve group and one Apex class under conflicts. What does sf project retrieve start do, and what should you do before using --ignore-conflicts?
Show answer
Without --ignore-conflicts, the retrieve stops because of the conflict on the Apex class, and nothing is overwritten. Before overriding, review the conflicting class, compare the org and local versions, and commit or stash your local edits. The override makes the org version replace your local file.
Exercise
Three components changed in the org. You run sf project retrieve start --metadata ApexClass:MyClass, and only MyClass is one of the three. What does the next sf project retrieve preview show, and why?
Show answer
It still lists the other two components as pending retrieves. A retrieve with an explicit selection advances tracking only for the components it fetched, so the unretrieved changes remain marked as changed in the org.