Sub topic 3
Why Salesforce Metadata Conflicts So Often
This chapter assumes you know basic Git (branches, commits, merge), have worked in a Salesforce DX project with a force-app directory, and can read simple XML. It explains why conflicts show up so often in metadata files. Later chapters cover how to resolve them.
How Git decides there is a conflict
Git merges text line by line. It compares each branch with their common ancestor. If only one branch changed a region, Git takes that change. If both branches changed the same region, or changed neighbouring lines, Git cannot choose and reports a conflict.
Git knows nothing about XML. It does not see elements such as <fieldPermissions>. It sees lines. Two changes that are logically independent can still collide if they touch the same lines.
Cause 1: One big file holds many unrelated settings
Some metadata types put a large amount of configuration in a single file. Profiles and permission sets are the main examples. A permission set such as Sales_Access.permissionset-meta.xml can hold field permissions, object permissions, Apex class access and tab settings. Every developer who adds a field or a class may need to touch it.
The more people edit one file, the more likely they are to edit the same region.
Cause 2: Sorted output puts new entries in the same place
The Salesforce CLI writes metadata in a stable order. Entries are sorted, usually alphabetically by element type and then by name. This is good for readable diffs. It also means that two developers adding entries that sort to the same spot will both insert text at the same position.
Look at the permission set from the Topic Spine example. One branch added a block for Account.Tier__c. The other added a block for Account.Segment__c. Both blocks went directly after Account.Region__c, so Git saw two different insertions at one location.
<<<<<<< HEAD
<fieldPermissions>
<editable>false</editable>
<field>Account.Tier__c</field>
<readable>true</readable>
</fieldPermissions>
=======
<fieldPermissions>
<editable>true</editable>
<field>Account.Segment__c</field>
<readable>true</readable>
</fieldPermissions>
>>>>>>> feature/new-fieldThe changes do not contradict each other. Both should be kept. Git still reports a conflict because it cannot know that.
Cause 3: Repeated closing tags make neighbouring edits overlap
XML repeats structure. Many blocks end with the same lines, such as </fieldPermissions>. When two branches add or remove adjacent blocks, the shared lines can confuse the line matching. Git may then treat the changes as overlapping, even when they are logically separate.
Cause 4: Retrieves rewrite more than you changed
Retrieving from an org can change files you did not intend to change. The org may return extra entries, reorder elements, or change whitespace. If you commit that output, your branch carries edits to many lines. Another branch that touched the same file now has a much larger overlap with yours.
A narrow retrieve, for example a single component rather than a whole profile, keeps your diff small.
Cause 5: Long-lived branches and shared sandboxes
The longer a branch lives, the more the base file drifts away from it. Admins who change the org through Setup, then retrieve, produce more changes to the same shared files. Several feature branches that each start from the same old commit all edit the same permission set, and each one conflicts with the others at merge time.
Summary of causes
| Cause | Where it shows up | Typical effect |
|---|---|---|
| Large multi-purpose files | Profiles, permission sets | Many people edit one file |
| Sorted output | Any list of entries | Insertions land at the same position |
| Repeated XML structure | Adjacent blocks | Overlapping line matches |
| Broad retrieves | Any retrieved file | Large unrelated diffs |
| Long-lived branches | Whole project | Drift between branches |
Exercise
Two developers each add one new field permission to the same permission set on separate branches. Both entries sort to the same position in the file. Why does Git report a conflict even though the changes do not contradict each other?
Show answer
Git compares lines, not XML elements. Both branches inserted text at the same position relative to the common ancestor, so Git cannot tell whether to keep one, both, or neither, and it reports a conflict. A person must read the file and decide, here by keeping both blocks.