Sub topic 6
Keeping both sides when changes are additive
This chapter assumes you can run git merge, read a conflict marker block, and open a file in the force-app directory of a Salesforce DX project. It also assumes you know basic XML structure: elements, nesting, and the rule that every opening tag needs a closing tag.
What "additive" means
A change is additive when one branch adds something and the other branch adds something different, and neither change depends on or replaces the other. Two developers each add a field permission to the same permission set. Two developers each add a custom label. Two developers each add a new <types> member to a package manifest. None of these edits touches the other's data.
Git does not know that. It compares text, not meaning. If both branches inserted lines at the same position in a file, Git cannot tell which insertion should come first, so it reports a conflict. The conflict is a text-ordering problem. Often there is no real disagreement between the two changes.
Metadata files make this common. Salesforce writes many elements in alphabetical or otherwise stable order, so two new entries that sort next to each other land at the same position. Salesforce also repeats the same element name many times in one file, which gives Git few unique lines to anchor on.
The workflow
- Run the merge and let it stop with a conflict.
- List the conflicted files.
- Open each file and find the marker blocks.
- Decide whether both sides are independent additions.
- Keep both sides, in a sensible order, and delete the three marker lines.
- Check that the XML is still well-formed.
- Stage the file and continue.
The diagram shows how the decision at step 4 branches.
Step 1: Find the conflicted files
A merge that cannot complete prints the files it could not combine. The second command gives a clean list you can work through.
git merge feature/new-field
# Auto-merging force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# CONFLICT (content): Merge conflict in force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# Automatic merge failed; fix conflicts and then commit the result.
git status
git diff --name-only --diff-filter=U--diff-filter=U limits the output to unmerged paths, which are the files that still need your attention. Files Git merged cleanly do not appear.
Step 2: Read the block
Here is the conflicted permission set. The current branch added a permission for Account.Tier__c. The incoming branch, feature/new-field, added one for Account.Segment__c.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<fieldPermissions>
<editable>true</editable>
<field>Account.Region__c</field>
<readable>true</readable>
</fieldPermissions>
<<<<<<< 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-field
<label>Sales Access</label>
</PermissionSet>The part between <<<<<<< HEAD and ======= is your side. The part between ======= and >>>>>>> feature/new-field is the incoming side. The Region__c block above the markers and the <label> below them are shared and unchanged.
Check the two sides against each other. They grant access to different fields. Neither block refers to the other. Each is a complete <fieldPermissions> element. That makes them independent additions.
Step 3: Keep both and remove the markers
The resolution is to keep both blocks and delete the three marker lines: the <<<<<<< line, the ======= line, and the >>>>>>> line. Nothing else changes.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<fieldPermissions>
<editable>true</editable>
<field>Account.Region__c</field>
<readable>true</readable>
</fieldPermissions>
<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>
<label>Sales Access</label>
</PermissionSet>Does the order matter?
Salesforce accepts <fieldPermissions> elements in any order on deployment. But the Salesforce CLI writes them sorted by field name when you retrieve. If you leave Tier__c before Segment__c, the next retrieve will reorder them and produce a diff that has nothing to do with real changes.
To keep later diffs small, put the blocks in the order the CLI would produce. Here that means alphabetical by <field>: Region__c, Segment__c, Tier__c. Swapping the last two blocks is a safe, optional cleanup. Do it if your team treats noisy diffs as a cost.
Edge cases in additive merges
Both sides add the same element
Suppose both branches add a permission for Account.Tier__c, each written slightly differently. Keeping both gives two <fieldPermissions> blocks for the same field. A deployment will fail or behave unpredictably because the file now contains a duplicate. This is a competing edit, not an additive one. Keep one block and decide which editable and readable values are correct. The next chapter covers that case.
The additions need a parent change
Sometimes an added element is only valid because of a change elsewhere in the same file. For example, a new <objectPermissions> block may only make sense if the object itself exists on the target branch. If the object was created on one branch only, the merged file may reference something that is not present. Git cannot warn you about this. A deployment dry run can.
Lists with a required order or a single-value element
Most repeating metadata elements are unordered lists. Some elements appear only once per file, such as <label> or <description>. If both sides changed a single-value element, you cannot keep both. You must pick one value. Treat that as a competing edit.
Custom labels and similar files
A CustomLabels.labels-meta.xml file holds many <labels> blocks in one file. Two branches that each add a label often conflict in the same way as the permission set above. The fix is the same: keep both <labels> blocks, make sure each <fullName> is unique, and remove the markers.
Step 4: Check before you stage
Do not stage a file just because the markers are gone. Run three quick checks.
# Check that no conflict markers remain
grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-app
# Check that a resolved file is still well-formed XML
xmllint --noout force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# Validate against the target org without saving changes
sf project deploy start --dry-run --source-dir force-app --target-org my-sandbox
# Finish the merge
git add force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git commit- The
grepcheck catches any marker you missed. A stray=======line is easy to leave behind in a long file. xmllint --nooutparses the file and prints nothing on success. It reports the line of the first syntax error otherwise.- The dry run asks the org to validate the deployment without saving it. It catches problems that are valid XML but invalid metadata, such as a reference to a missing field.
git commit with no message opens your editor with a pre-filled merge message. Saving it completes the merge.
Common mistakes
| Mistake | Result | Prevention |
|---|---|---|
| Deleting only some marker lines | XML parse error or leftover text in the file | Run the `grep` check |
| Deleting one side by accident | A teammate's permission is silently lost | Compare the result against both branches with `git diff` |
| Keeping duplicate elements | Deployment error or unpredictable access | Search the file for each `<field>` or `<fullName>` value |
| Breaking indentation or tags while editing | Malformed XML | Run `xmllint --noout` |
| Skipping the dry run | Reference errors appear later in CI or production | Always validate against a sandbox |
Exercise
Exercise
A merge conflict in CustomLabels.labels-meta.xml shows two different <labels> blocks. One has <fullName>Welcome_Message</fullName> and the other has <fullName>Goodbye_Message</fullName>. What do you do, and what do you check afterward?
Show answer
The two blocks have different fullName values, so they are independent additions. Keep both <labels> blocks and delete the <<<<<<<, =======, and >>>>>>> lines. Optionally order them alphabetically by fullName to match CLI output. Afterward, run the grep check for leftover markers, run xmllint --noout on the file to confirm it is well-formed, and run a deployment dry run against a sandbox. Then stage the file and commit.
Exercise
Both branches added a <fieldPermissions> block for Account.Tier__c. One sets editable to true and the other sets it to false. Is this an additive conflict? Why or why not?
Show answer
No. Both sides edit the same field, so keeping both would create a duplicate element. This is a competing edit. You must choose the correct editable value, keep a single block, and confirm the choice with the author of the other branch if needed.