Sub topic 8
Handling metadata merge conflicts
This chapter assumes you know basic Git merging, can read Salesforce metadata XML, and have a feature workflow like the one in the previous chapter. It covers why metadata conflicts happen, how to resolve them, and how to make them rarer.
Why metadata conflicts happen
Salesforce metadata is XML. Two developers rarely edit the same Apex line, but they often touch the same XML file. Profiles, permission sets, page layouts, flows, and custom labels collect many unrelated settings in one file. Two branches that each add one entry to the same file can collide, even though the changes do not depend on each other.
Two patterns cause most conflicts:
- Shared container files. A profile or layout lists many objects and fields. Every feature that adds a field edits it.
- Reordering. Retrieving from an org can reorder elements or change whitespace. A branch that only retrieved a file now shows a large diff and conflicts with others.
Resolving a conflict
Resolve conflicts on your feature branch, not on main. Bring main into your branch, fix the file, and push.
git fetch origin
git merge origin/main
# CONFLICT (content): Merge conflict in force-app/main/default/permissionsets/Case_Agent.permissionset-meta.xml
git statusOpen the file. Git marks the two sides:
<<<<<<< HEAD
<fieldPermissions>
<editable>true</editable>
<field>Case.Routing_Queue__c</field>
<readable>true</readable>
</fieldPermissions>
=======
<fieldPermissions>
<editable>true</editable>
<field>Case.Escalation_Level__c</field>
<readable>true</readable>
</fieldPermissions>
>>>>>>> origin/mainBoth entries are valid and independent, so keep both. Remove the three marker lines and leave both fieldPermissions blocks. Then check the result before committing.
git add force-app/main/default/permissionsets/Case_Agent.permissionset-meta.xml
git commit
sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTests --target-org case-routingThe check-only deploy matters here. Valid-looking XML can still be wrong. A leftover marker, a duplicated element, or a field that exists on only one branch will fail at deployment, not in Git.
When two branches change the same value, for example the same <editable> flag, you must choose one. Ask the author of the other change instead of guessing.
Reducing how often conflicts occur
Resolution is a recovery step. These habits prevent most conflicts:
- Keep branches short. The longer a branch lives, the more main moves under it. Merge main into your branch daily.
- Retrieve only what you changed. Run
sf project retrieve startwith--metadataor--source-dirfor the components you touched. A full retrieve brings in unrelated reordering. - Prefer permission sets over profiles. Many small permission sets spread edits across files. One large profile concentrates them.
- Review diffs before committing. If a file shows hundreds of changed lines for a one-field change, discard the noise with
git restoreand keep only the real edit. - Announce shared-file work. If two people must edit the same flow, agree on who goes first. Flows are hard to merge by hand because the XML describes a graph.
Flows and other hard cases
For flows, do not merge the XML by hand unless the change is trivial. Pick one version with git checkout --theirs <file> or git checkout --ours <file>, then redo the other change in Flow Builder in a scratch org and retrieve it again. In a merge, "ours" is your branch and "theirs" is main.
Exercise
Your branch and main both added a field permission to the same permission set, for different fields. After you remove the conflict markers, what should you check before pushing, and why?
Show answer
Run a check-only deployment. Git only checks text, so a leftover marker, duplicate element, or reference to a field missing in the target org passes Git but fails at deployment.