Sub topic 8
Retrieving changes made in the org
This chapter assumes you have a Salesforce DX project, the sf CLI installed, and a tracked org (a scratch org or a tracking-enabled sandbox) already authorized. You should also know how to preview changes, which the previous chapter covered.
Why retrieve from the org
Not every change starts in your editor. You might build a flow in Setup, adjust a page layout by clicking, or have a teammate edit a component in a shared sandbox. These edits exist only in the org until you bring them into your project. In a tracked org, the CLI records each of them as a remote change, and you can pull only those changes into your local files.
Preview first
Always look before you retrieve. The preview reads the tracking data and changes nothing.
sf project retrieve preview --target-org my-scratchThe output groups components into three kinds of entries:
- Changes in the org that a retrieve would bring down.
- Local changes that a deploy would send. These are not touched by a retrieve.
- Conflicts, meaning components changed both locally and in the org.
If the list shows a component you did not expect, find out why before you continue.
Retrieve the changes
In a tracked org, you do not need any source flags. The CLI retrieves only what changed in the org since the last sync.
sf project retrieve start --target-org my-scratchThe command writes the changed components into your package directories, such as force-app/main/default. It then updates its local tracking state, so the next preview no longer lists those components.
Run git status or git diff after a retrieve. Retrieved files often show formatting changes, for example reordered XML elements in a profile. Review them before you commit.
Retrieving specific components
You can still limit a retrieve by naming what you want. This works in any org, tracked or not.
sf project retrieve start --target-org my-scratch --metadata ApexClass:MyClassNaming components is the only option in an untracked org, because the CLI cannot work out what changed there. A later chapter covers that workflow.
What a retrieve does not do
A retrieve skips anything matched by your .forceignore file. If a change you made in the org never appears locally, check that file first.
A retrieve also does not remove local files for components that were deleted in the org. Deletes are handled separately, and the chapter on deletes and renames covers them.
Common errors
- Conflict reported. You changed the same component locally and in the org. The retrieve stops instead of overwriting your local file. Review the preview output and decide which version to keep. Use
--ignore-conflictsonly when you are sure the org's version should win. - Nothing to retrieve, but you know the org changed. The change may be matched by
.forceignore, or the component type may not be tracked. Another cause is a target org that does not support tracking. Check the alias withsf org list. - Unexpected components appear. Other users or automated processes may have edited the org. Preview shows the exact list, so read it before retrieving.
Exercise
You edited a Lightning page in Setup in your scratch org. You also have uncommitted changes to an Apex class in your project. What do you run first, and what do you expect it to show?
Show answer
Run sf project retrieve preview --target-org my-scratch. It should list the Lightning page as a remote change that a retrieve would bring down. The Apex class appears as a local change only, and a retrieve does not touch it. After checking the list, run sf project retrieve start --target-org my-scratch to pull the page into your project.