Sub topic 3
Why Salesforce metadata conflicts so often
This chapter assumes you can branch, commit and merge in Git, and that you know the layout of a Salesforce DX project under force-app. It also assumes you can read basic XML. The goal is to understand the causes of metadata conflicts, so that you can predict where they will appear and later resolve or prevent them.
Git merges lines, not meaning
Git's default merge works on lines of text. It compares each branch with their common ancestor and collects the changed regions (hunks). If two hunks touch different parts of the file, Git combines them. If two hunks overlap, or sit directly next to each other with no unchanged line between them, Git cannot decide the order and reports a conflict.
Git has no idea that the file is XML or that two <fieldPermissions> blocks are independent. Two people can make changes that mean no conflict to a human and still collide in Git. Most Salesforce metadata conflicts come from this gap.
Source format keeps files line-oriented
A Salesforce DX project stores metadata in source format. Elements are pretty-printed, usually one value per line, and the Salesforce CLI writes them in a consistent order. This is why Git can merge metadata at all, but it also concentrates changes in predictable places.
Many types, such as Apex classes and Lightning components, are one component per file or folder. Two people rarely touch the same one. Other types gather many unrelated settings into a single file, and those are the ones that conflict.
| Metadata type | Typical file | Why it conflicts |
|---|---|---|
| Profile | `profiles/Admin.profile-meta.xml` | One file holds field, object, Apex class and page access for the whole org |
| Permission set | `permissionsets/Sales_Access.permissionset-meta.xml` | Every new field or object usually adds an entry to the same file |
| Custom labels | `labels/CustomLabels.labels-meta.xml` | All labels live in one file by default |
| Flow | `flows/Lead_Routing.flow-meta.xml` | Elements are tightly linked and the file is large |
| Page layout | `layouts/Account-Account Layout.layout-meta.xml` | Section and field ordering changes shift many lines |
Some types are already split into smaller files. A custom object, for example, is stored as one file per field under objects/Account/fields/. Salesforce has also introduced optional decomposition for some of the large types, enabled through sourceBehaviorOptions in sfdx-project.json. Check the current Salesforce documentation for which types support it, because the options change between CLI releases.
Cause 1: Many authors edit one aggregate file
A single feature rarely touches only its own files. Adding a custom field is a typical case:
- The field itself goes into a new file, which cannot conflict.
- The field's access goes into a profile or permission set, which several other branches also edit.
- A new label, if the feature has one, goes into
CustomLabels.labels-meta.xml.
The new files merge cleanly, and the shared files do not. The more branches are open at once, the more of them edit the same few aggregate files. The number of conflicts grows with the number of parallel branches.
Cause 2: Sorted output means insertions land together
The CLI writes list elements in a stable order, usually alphabetical by name. This is good for diffs, because the same metadata always produces the same file. It also means that unrelated additions often land at the same spot.
Suppose two branches each add a label whose name falls between the same two existing labels. Both branches insert a block at the same position, and Git sees two insertions at one location. You can reproduce this with a short script. It builds a labels file with one label, then adds a different label on each of two branches:
git init -b main labels-demo && cd labels-demo
mkdir -p force-app/main/default/labels
f=force-app/main/default/labels/CustomLabels.labels-meta.xml
cat > "$f" <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
<labels>
<fullName>Invoice_Header</fullName>
<language>en_US</language>
<protected>false</protected>
<shortDescription>Invoice header</shortDescription>
<value>Invoice</value>
</labels>
</CustomLabels>
EOF
git add . && git commit -m "Base labels"
# Insert a new label block after line 9, the end of the first label
add_label() {
cat > /tmp/label.xml <<EOF
<labels>
<fullName>$1</fullName>
<language>en_US</language>
<protected>false</protected>
<shortDescription>$2</shortDescription>
<value>$2</value>
</labels>
EOF
sed '9r /tmp/label.xml' "$f" > "$f.new" && mv "$f.new" "$f"
}
git checkout -b feature/a
add_label Order_Limit "Order limit"
git commit -am "Add Order_Limit label"
git checkout main && git checkout -b feature/b
add_label Order_Notice "Order notice"
git commit -am "Add Order_Notice label"
git merge feature/a
# CONFLICT (content): Merge conflict in force-app/main/default/labels/CustomLabels.labels-meta.xmlThe two labels have different names and do not depend on each other, yet the merge stops. The next chapter shows how to read the markers it leaves behind.
Cause 3: Repeated closing tags confuse the alignment
Metadata files repeat the same lines many times. A permission set contains dozens of </fieldPermissions>, <editable>true</editable> and <readable>true</readable> lines. When Git computes a diff, it matches unchanged lines between versions. With so many identical lines, it can pair a new block with the wrong neighbour.
The result is a conflict whose boundaries look odd. The markers may split a block in the middle, or one side may appear to include a closing tag that belongs to its neighbour. The content is still correct, but you must read it as whole XML elements, not as the lines Git chose to highlight.
Cause 4: Retrieves from an org create changes nobody wrote
Not every change in a diff comes from a developer editing a file. Metadata that is retrieved from an org can differ from what is in Git, even when nobody changed the configuration on purpose:
- A profile retrieve only includes the entries for components retrieved in the same request. Retrieving with different manifests produces different profile files, with entries appearing or vanishing.
- A newer API version in
sfdx-project.jsonor in a retrieve can reformat elements or add new ones. - An administrator working in a shared sandbox changes access, and the next retrieve captures it along with everything else.
- Different CLI versions may order or format some elements differently.
Two developers who retrieve the same profile from different orgs can commit large, overlapping diffs. A conflict then arises even though neither of them edited the file by hand.
Cause 5: Long-lived branches grow the overlap
The longer a branch lives, the more of its changes collide with changes on the main branch. A branch open for three weeks can touch the same profile as four other branches that have since merged. Each of them moved the surrounding lines. The conflict is then larger than any single change, and it is harder to tell which side is right.
Finding your project's hot spots
Your project will have its own set of files that conflict repeatedly. Git history shows which ones change most often:
git log --since="90 days ago" --name-only --pretty=format: -- force-app \
| sort | uniq -c | sort -rn | head -15Files near the top of this list are usually profiles, permission sets and labels. They are where prevention work pays off most, and later chapters cover it.
Summary of the mechanism
A metadata conflict needs three conditions together, and removing any one of them reduces conflicts:
- Several branches edit one shared file, because the type is stored as an aggregate.
- Their edits fall in the same or neighbouring lines, because output is sorted and blocks repeat.
- Git cannot tell that the edits are independent, because it merges text, not XML elements.
Exercise
Two developers each add a new custom field and grant access to it in the same permission set file, on separate branches. The fields themselves are in different files. Which merge conflicts, if any, should you expect, and why?
Show answer
The two new field files merge cleanly, because each branch creates a separate file. The permission set file is likely to conflict. Both branches add a <fieldPermissions> block to one file, and because the CLI sorts entries, the new blocks often land at the same or adjacent positions. Git sees two overlapping or touching hunks and cannot tell they are independent.
Exercise
A developer who made no edits to a profile finds a large conflict in it after a merge. Name two ways this can happen.
Show answer
The branch may have included a profile that was re-retrieved from an org, so it contained entries that differ from Git even though nobody edited it by hand. This can come from retrieving with a different manifest, a different API version or a different CLI version. A long-lived branch can also have overlapping changes with other branches that have merged since it started.