Sub topic 8
Handling metadata merge conflicts
This chapter assumes you can commit, branch, merge, and open a pull request. It also assumes you know what Apex classes, custom objects, flows, and profiles look like as files in a Salesforce DX project. Earlier chapters set up the force-app directory and a feature workflow built on short-lived branches. Here you will learn why Salesforce metadata conflicts differ from ordinary code conflicts, how to resolve them, and how to make them less frequent.
Why metadata conflicts happen
Git merges text line by line. Two branches conflict when they change the same lines, or lines that sit next to each other, in the same file. Most Apex code is spread across many small files, so conflicts are rare. Salesforce metadata is different, for three reasons.
- Large XML files hold many unrelated settings. A profile, permission set, or flow is one file. Two developers working on different features can both edit it.
- Generated order and formatting. Retrieved metadata is written by the platform. If the order of elements changes between retrievals, Git sees changed lines that nobody wrote on purpose.
- Shared files collect every change. Files such as
package.xml, the layout for a busy object, and the Lightning page for a record all attract edits from many branches.
The result is that a conflict in metadata is often not a disagreement about logic. It is two people adding different entries to the same list.
Where conflicts cluster
The table shows the file types that conflict most often and the usual cause.
| Metadata type | File location | Typical cause of conflict |
|---|---|---|
| Profiles | `profiles/*.profile-meta.xml` | Many features add field and class permissions to the same file |
| Permission sets | `permissionsets/*.permissionset-meta.xml` | Two features add access to the same object |
| Flows | `flows/*.flow-meta.xml` | Node positions and element order change on every save |
| Page layouts | `layouts/*.layout-meta.xml` | Two branches add fields to the same section |
| Custom object files | `objects/Account/Account.object-meta.xml` | Shared settings such as sharing model or search layouts |
| Custom labels | `labels/CustomLabels.labels-meta.xml` | One file holds every label in the project |
Compare that with an Apex class such as CaseRouter.cls. It is owned by one feature most of the time, and a conflict there is a normal code conflict.
What a metadata conflict looks like
Suppose two branches each grant access to a new field in the Support_Agent permission set. After you merge the second branch, Git leaves markers in the file.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<fieldPermissions>
<editable>true</editable>
<field>Case.Routing_Priority__c</field>
<readable>true</readable>
</fieldPermissions>
<<<<<<< HEAD
<fieldPermissions>
<editable>false</editable>
<field>Case.Escalation_Reason__c</field>
<readable>true</readable>
</fieldPermissions>
=======
<fieldPermissions>
<editable>true</editable>
<field>Case.Customer_Tier__c</field>
<readable>true</readable>
</fieldPermissions>
>>>>>>> feature/customer-tier
<label>Support Agent</label>
</PermissionSet>Both sides added a new fieldPermissions block. Neither change is wrong. The correct result keeps both blocks. This is the most common metadata conflict, and the fix is to keep both sides.
Resolving a conflict step by step
Work on your own feature branch, not on main. Bring in the latest main and resolve the conflict locally, so the pull request merges cleanly.
git switch feature/escalation-reason
git fetch origin
git merge origin/main
# Git reports: CONFLICT (content): Merge conflict in force-app/main/default/permissionsets/Support_Agent.permissionset-meta.xml
git status
# Open the file, keep both fieldPermissions blocks, remove the markers
git add force-app/main/default/permissionsets/Support_Agent.permissionset-meta.xml
git commitAfter you commit the merge, do not stop. A file that is valid XML and has no markers can still fail on deployment. Validate the result against an org.
sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTests --target-org case-routing --wait 60The --dry-run flag performs a check-only deployment, so nothing is saved in the org. If it passes, push the branch.
Resolution rules by metadata type
Not every conflict is solved by keeping both sides. Use the type of file to decide.
Additive lists
Permission entries, field lists in layouts, and custom label entries are lists. Keep both sides. Check that you have not created a duplicate entry. Salesforce rejects a profile or permission set that grants the same field twice.
Flows
A flow file holds element definitions and the canvas position of each element. Two people editing the same flow produce conflicts that are hard to read, because the order of elements can change. Do not try to merge the XML by hand unless the conflict is small. A safer approach:
- Pick one version of the flow, either yours or
main. - Open that version in Flow Builder in a scratch org.
- Re-apply the other person's change by hand.
- Retrieve the result and commit it.
Agree on one owner for each flow. Two people should not edit the same flow in parallel.
Profiles
Profiles are the worst case. A profile can contain thousands of lines, and a retrieve from a different org can return a different subset of them, because profile retrieval depends on which other metadata is in the request. If the conflict is large, take the version from main and re-add only your own permission entries.
Where you can, use permission sets for new access. They are smaller, they conflict less, and they are easier to review.
Generated and ordering conflicts
If the conflict shows only reordered elements or moved whitespace, no one changed the logic. Take one side. Then retrieve again from the org to confirm the ordering the platform produces, and commit that.
You can ask Git to choose one side for a whole file:
git checkout --theirs force-app/main/default/flows/Case_Routing.flow-meta.xml
git add force-app/main/default/flows/Case_Routing.flow-meta.xmlIn a merge, --ours is your current branch and --theirs is the branch being merged in. In a rebase, the two are swapped. Check which one you have before using either option.
Reducing how often conflicts occur
Resolving conflicts is a cost. Lowering their number is cheaper.
- Keep branches short. The longer a branch lives, the more
mainmoves under it. Merge within days, as the branch lifecycle chapter describes. - Merge
maininto your branch often. A small, frequent merge is easier than one large merge on the last day. - Prefer permission sets to profiles. Put new field and class access in a permission set owned by the feature.
- Split large files. Use one permission set per feature or per persona instead of one shared set. Custom labels and similar shared files can be reduced by using Custom Metadata or Custom Settings where that makes sense.
- Assign owners. For flows and layouts, a single person or team makes changes, and others ask them.
- Retrieve only what you changed. Pulling an entire org into your branch adds noise. Retrieve the specific components.
- Keep formatting stable. Use the same CLI version across the team, and consider a formatter such as Prettier with the Apex and XML plugins, applied through a pre-commit hook, so that generated files are written the same way every time.
sf project retrieve start --metadata "PermissionSet:Support_Agent" --target-org case-routingRetrieving a named component keeps the diff small, so a reviewer sees only the lines you meant to change.
Common errors
- Resolving on
main. If you merge a feature intomainlocally and resolve there, you bypass the pull request. Resolve on the feature branch. - Keeping markers. A leftover
<<<<<<<line makes the XML invalid. Search the project before you commit. - Duplicate entries. Keeping both sides can produce the same permission twice. The check-only deploy catches this.
- Resolving without testing. The merge may be valid text and still break behavior. A flow with a missing element reference fails only when deployed or run.
- Rebasing a shared branch. Rebasing rewrites history. If other people use the branch, a rebase forces them to repair their copy. Prefer merging
maininto a shared branch.
You can find leftover markers before committing with a short search:
git grep -n -E '^(<<<<<<<|=======|>>>>>>>)' -- force-appIf the command prints nothing, no markers remain. Git exits with status 1 in that case.
Summary
Metadata conflicts come from large shared XML files such as profiles, permission sets, and flows. Most are additive and are fixed by keeping both sides. Flows and profiles need more care, and sometimes a rebuild in an org. Always validate with a check-only deployment, and lower the number of conflicts with short branches, frequent merges, small permission sets, and clear file ownership.
Exercise
Two feature branches each add a new fieldPermissions block to Support_Agent.permissionset-meta.xml. You merge origin/main into your branch and Git reports a conflict in that file. Describe how you resolve it and how you verify the result before pushing.
Show answer
Open the file and keep both fieldPermissions blocks, because each branch added a different field, then delete the conflict markers. Check that no field appears twice. Stage the file and commit the merge on your feature branch, not on main. Run sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTests --target-org <alias> to confirm the check-only deployment passes, then push the branch so the pull request updates.
Exercise
Two developers both edited Case_Routing.flow-meta.xml, and the conflict shows many moved elements and changed canvas positions. Why is a manual XML merge risky here, and what is a safer approach?
Show answer
The file stores element definitions and their canvas positions, and the order can change on every save, so a hand merge can drop or misconnect elements while still producing valid XML. A safer approach is to choose one version of the flow, open it in Flow Builder in a scratch org, re-apply the other change by hand, retrieve the flow, and commit it. Going forward, give the flow a single owner.