Sub topic 11
Settling a hard conflict by re-retrieving from an org
This chapter assumes you can read conflict markers, run git merge, and use sf project retrieve start against a sandbox. Sometimes a conflict is so tangled that editing it by hand is riskier than starting over. In that case you can let an org supply the correct file.
When re-retrieving makes sense
Re-retrieving works when the org is the real source of truth for the file. These are typical cases:
- A generated or reordered file, such as a layout or a large profile, where both branches touched many lines.
- A conflict where you cannot tell which lines belong to which change.
- A file that is already correct in a sandbox, because both features were deployed and tested there.
It is a poor choice when the org does not contain both branches' changes. A retrieve copies the org's version over your working file. Any change the org lacks is silently lost.
The procedure
- Confirm which org holds the combined state. If it does not exist yet, deploy both branches' changes to a sandbox first.
- Retrieve the file. The command overwrites the conflicted file, including its markers.
- Compare the result with each side of the merge.
- Stage the file and finish the merge.
# Overwrite the conflicted file with the org's version
sf project retrieve start \
--metadata PermissionSet:Sales_Access \
--target-org my-sandbox
# Confirm no markers remain
grep -nE '^(<<<<<<<|=======|>>>>>>>)' \
force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# Compare with our side and with the branch being merged in
git diff HEAD -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git diff MERGE_HEAD -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlHEAD is the branch you are merging into. MERGE_HEAD is the branch you are merging in. A good result differs from each of them only by the other side's changes. If either diff removes lines you expected to keep, the org was missing something.
Reviewing the retrieved file
The org may write elements in a different order or add entries that nobody on your team changed. Check these points before you stage:
- Every field, class, or component added on either branch is present.
- No unrelated entries appear that came from other changes in the sandbox.
- The XML is still well-formed. Run
xmllint --noouton the file.
Finishing the merge
Once the diffs look right, stage the file and validate before you commit.
git add force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
sf project deploy start --dry-run --source-dir force-app --target-org my-sandbox
git commitIf the dry run fails or the diffs look wrong, you can start over. git merge --abort returns you to the state before the merge. Your retrieved file is discarded with it, so nothing is lost beyond the retrieve itself.
Common errors
| Symptom | Likely cause |
|---|---|
| Retrieved file lacks one branch's change | The org never received that change |
| Large unrelated diff after retrieve | The org has changes from other work, or orders elements differently |
| Retrieve reports nothing retrieved | Wrong metadata name or wrong org alias |
| Git still lists the file as unmerged | You have not run `git add` on it yet |
Exercise
You merge feature/new-field and the Sales_Access permission set conflicts. Your sandbox has only the main branch deployed. You retrieve the permission set and the markers disappear. Why is it unsafe to commit now, and what should you do first?
Show answer
The sandbox does not contain the feature branch's changes, so the retrieve overwrote the file with a version that lacks them. The markers are gone, but the new field permission is lost. Deploy the feature branch's metadata to the sandbox (or resolve the file by hand), retrieve again, and check git diff MERGE_HEAD for the missing entries before you stage the file.