Sub topic 11
Settling a hard conflict by re-retrieving from an org
This chapter assumes you can run sf commands against a sandbox or scratch org, read conflict markers, and keep XML well-formed. It covers the case where hand-merging a file costs more than it is worth. In that case you can let an org supply the correct content instead.
When hand-merging stops being worth it
Some conflicts are small and obvious. Two people added a field permission, and you keep both blocks. Other conflicts are not like that. A flow with 40 elements has been rearranged on both branches. A profile has hundreds of changed lines because two developers retrieved it from different orgs. A page layout has moved sections around on each side. The markers cover large regions, and you cannot tell by reading which lines matter.
In these cases, ask a different question. Instead of asking how to merge the text, ask what the metadata should look like when it is correct. An org that holds the intended configuration can answer that question. You retrieve the file from it and let that result replace the conflicted file.
This approach only works if an org really does hold the intended state. If it does not, a retrieve gives you the wrong content with a clean file format. That is worse than a conflict, because nothing warns you.
Deciding whether to re-retrieve
Use the following checks before you choose this path.
- Is the metadata type retrievable and stable? Layouts, flows and profiles retrieve well. Types that the org rewrites on every retrieve produce noisy output, but the content is still correct.
- Does one org represent the merged intent? A shared integration sandbox that already has both features deployed is a good source. A developer's scratch org that has only one feature is not.
- Is the file mostly generated? If people rarely edit the file by hand and mostly produce it through Setup, re-retrieving is natural.
- Would a retrieve overwrite work that exists only in Git? If so, do not re-retrieve. Merge by hand.
| Situation | Recommended action |
|---|---|
| Two additive field permissions | Keep both blocks by hand |
| Both sides reordered a layout | Re-retrieve from an org that has the merged layout |
| Flow edited in two sandboxes | Pick one version, or rebuild in an org and retrieve |
| Profile has hundreds of conflicting lines | Re-retrieve, then check the diff |
| Apex class with a code conflict | Merge by hand. Never re-retrieve code you have not deployed |
| Unsure which branch is right | Abort the merge and ask |
The workflow
The goal is to end with a merge commit whose conflicted file came from an org. The steps below keep your options open at each stage.
- Note which files are conflicted.
- Decide which side to start from, and take it whole.
- Deploy that content to a sandbox or scratch org that you can safely change.
- Adjust the configuration in the org so it reflects both features.
- Retrieve the file back into the project.
- Review the diff against both parents.
- Stage the file and finish the merge.
# 1. See what is conflicted
git diff --name-only --diff-filter=U
# 2. Take one side whole, for example the branch you are merging in
git checkout --theirs -- force-app/main/default/layouts/Account-Account\ Layout.layout-meta.xml
# 3. Deploy it to a sandbox you control
sf project deploy start \
--source-dir "force-app/main/default/layouts/Account-Account Layout.layout-meta.xml" \
--target-org my-sandboxAt this point the org holds one side. Open the org, add whatever the other side contributed (for example, the new section from the other feature), and save. Then retrieve.
# 5. Pull the combined result back into the project
sf project retrieve start \
--metadata "Layout:Account-Account Layout" \
--target-org my-sandboxThe retrieve overwrites the file in your working tree. Git still treats the path as conflicted until you stage it.
# 6. Compare the result against each parent
git diff HEAD -- "force-app/main/default/layouts/Account-Account Layout.layout-meta.xml"
git diff MERGE_HEAD -- "force-app/main/default/layouts/Account-Account Layout.layout-meta.xml"
# 7. Mark it resolved and finish
git add "force-app/main/default/layouts/Account-Account Layout.layout-meta.xml"
git commitHEAD is the branch you are on, and MERGE_HEAD is the branch you are merging in. A good result shows changes from both when you diff against each one. If the diff against HEAD shows only the other branch's work, and the diff against MERGE_HEAD shows only yours, the merge is correct.
Choosing the source org
The retrieved content is only as good as the org you pull from. Think about three properties.
It must contain the merged intent. If the org was built from only one branch, the retrieve drops the other branch's changes. Treat the org as an input you have to prepare, as step 4 does.
It must match the project's API version and features. An org on a newer release can add elements that your project's sourceApiVersion does not expect. Check sfdx-project.json and look at the retrieved <apiVersion> values in the diff.
It must not hold unrelated changes. A shared sandbox may include half-finished work from other people. Retrieving a profile from it brings that work into your commit. A scratch org created from the merge result is the cleanest option, because you control everything in it.
# Create a scratch org, push the merged-so-far project, then adjust and retrieve
sf org create scratch --definition-file config/project-scratch-def.json --alias merge-check
sf project deploy start --target-org merge-checkProfiles and permission sets need extra care
Profiles are the hardest case. A retrieve returns only the permissions for the components that are in the same retrieve request. If you retrieve a profile alone, you may get a nearly empty file. Retrieve it together with the objects, fields and Apex classes it should describe.
sf project retrieve start \
--metadata "Profile:Admin" \
--metadata "CustomObject:Account" \
--metadata "ApexClass" \
--target-org my-sandboxPermission sets behave better, because they list only what they grant. They still depend on the org having the fields and classes they refer to.
After a profile retrieve, review the diff in full. Look for permissions that disappeared. A dropped fieldPermissions block means the org did not know about that field, not that you wanted it removed.
Reviewing the retrieved file
A retrieve often changes more than the feature required. The org may reorder elements, add default values, or update the API version. Separate real changes from noise.
- Compare against
MERGE_HEADandHEAD, as shown above, and read each hunk. - Revert unrelated hunks with
git checkout -p, or edit them out. - Check that no component appears in the retrieved file that your project does not contain.
- Run the checks from the earlier chapter: no leftover markers, valid XML, and a dry-run deployment.
grep -nE '^(<<<<<<<|=======|>>>>>>>)' "force-app/main/default/layouts/Account-Account Layout.layout-meta.xml"
xmllint --noout "force-app/main/default/layouts/Account-Account Layout.layout-meta.xml"
sf project deploy start --dry-run --source-dir force-app --target-org my-sandboxFailure modes
- Silent loss of a branch's work. The org did not have one side's change, so the retrieve erased it. Always diff against both parents.
- Overwriting someone else's org changes. If you deploy to a shared sandbox in step 3, you replace what is there. Use a personal org.
- Source tracking confusion. After deploy and retrieve, tracking in the org may show changes that no longer match Git. A later chapter covers how this differs from a Git conflict.
- Retrieved file in the wrong format. If the retrieve writes to a different package directory than the original, you end up with two copies. Check
git statusfor new untracked files. - Environment-specific values. Some metadata contains values tied to an org, such as user names or IDs. Search the retrieved diff for them before committing.
Exercises
Exercise
You merge a branch and a profile file has 300 conflicting lines. A teammate suggests retrieving Profile:Admin alone from the shared sandbox and committing it. Name two problems with that plan and what you would do instead.
Show answer
First, a profile retrieved alone may omit permissions for objects, fields and classes that are not part of the same request, so the file can lose content. Second, the shared sandbox may not contain both branches' changes and may hold other people's unfinished work. Instead, prepare a personal or scratch org with the merged project, retrieve the profile together with the related objects, fields and classes, and diff the result against both HEAD and MERGE_HEAD before staging it.
Exercise
After retrieving a layout, git diff HEAD shows your branch's section added, but git diff MERGE_HEAD shows nothing about the incoming branch's new field. What went wrong, and what do you do?
Show answer
The org did not contain the incoming branch's change, so the retrieve dropped it. Return to the org, add the missing field to the layout, retrieve again, and repeat the diffs until each shows only the other branch's work.