Sub topic 8
Resolving conflicts in profiles and permission sets
This chapter assumes you can run basic Git merges, work in a Salesforce DX project with source format, read XML, and use the sf CLI against a sandbox or scratch org. Earlier chapters covered conflict markers and keeping XML valid. Here you apply them to the two file types that conflict most often.
Why these files conflict so much
Profiles and permission sets list access settings as repeated child elements. A permission set such as Sales_Access.permissionset-meta.xml contains fieldPermissions, objectPermissions, classAccesses, userPermissions, tabSettings and more. Every field, object or Apex class that a user needs adds another block.
Two things make this a conflict hotspot:
- Alphabetical ordering. The CLI writes child elements in a stable, sorted order. Two developers who each add a field tend to add entries at the same sorted position, so Git sees two edits at the same spot.
- Many people touch one file. A single permission set often grants access to many features. Each feature branch edits it.
Profiles are worse than permission sets. A profile can hold thousands of lines, and a retrieve can rewrite parts of it that you did not mean to change. Many teams grant access through permission sets and keep profiles minimal for this reason.
Anatomy of the conflict
Suppose main added access to Account.Tier__c and feature/new-field added access to Account.Segment__c. Merging the feature branch stops like this:
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=UThe last command lists only files that still have unresolved conflicts. Use it as your to-do list.
Open the file. Each side added a whole fieldPermissions block at the same position:
<?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 section between <<<<<<< HEAD and ======= is your current branch. The section between ======= and >>>>>>> feature/new-field is the incoming branch.
Decision: keep both, pick one, or re-retrieve
Before you edit, decide which of three cases you have.
| Situation | Action |
|---|---|
| Each side added a different element (different `field`, `object`, or `apexClass` value) | Keep both blocks |
| Both sides changed the same element, such as `editable` for the same field | Pick one value deliberately |
| The file is a mess, or you cannot tell what each side intended | Re-retrieve from an org (covered in a later chapter) |
The test is the identifying child element. In fieldPermissions, the <field> value identifies the entry. Different values mean independent additions. Identical values mean a competing edit.
Resolving an additive conflict
Here the two fields differ, so keep both. Delete the three marker lines and leave both blocks intact:
<?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>The order of sibling fieldPermissions blocks does not change behavior. Salesforce does not care whether Segment__c comes before Tier__c. The next retrieve will re-sort the entries, which produces a small diff later. That is harmless, and you do not need to sort by hand.
Resolving a competing edit
Now suppose both branches changed the same field. Main set Account.Tier__c to read-only, and the feature branch made it editable:
<<<<<<< HEAD
<fieldPermissions>
<editable>false</editable>
<field>Account.Tier__c</field>
<readable>true</readable>
</fieldPermissions>
=======
<fieldPermissions>
<editable>true</editable>
<field>Account.Tier__c</field>
<readable>true</readable>
</fieldPermissions>
>>>>>>> feature/new-fieldKeeping both would leave two entries for the same field. A deployment with duplicate entries can fail or apply one unpredictably. Choose one block and delete the other. Which one is right is a business question, not a Git question. Ask the authors, or check the commit messages and work items.
To see who changed what, compare each side to the common ancestor:
git log --merge --oneline -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git diff --base -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlgit log --merge shows only the commits from both branches that touch the conflicted file. git diff --base shows your side against the merge base.
Conflicts that Git does not flag
Git merges by text lines. It can produce a clean merge that is still wrong for Salesforce. Two cases appear often:
- Both branches add the same
fieldPermissionsentry in slightly different places. Git may merge both without a conflict, leaving a duplicate. - One branch deletes a field and its permission entry, while the other branch edits that entry. You may get a conflict where one side is empty.
After every merge that touched a profile or permission set, scan for duplicates even if Git reported no conflict. This command lists repeated field names in one file:
grep -o '<field>[^<]*</field>' force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml | sort | uniq -dNo output means no duplicate fields. Run the same idea for <apexClass>, <object> and <tab> values.
Profiles: extra cautions
Profiles behave like permission sets but carry more risk:
- Large retrieves rewrite unrelated sections. If a branch includes an accidental full-profile retrieve, you will see conflicts in areas unrelated to your work. Prefer the incoming side only for the section you actually care about, and review the rest.
- Taking one whole side loses work.
git checkout --theirson a profile discards every change made on your branch. Use it only when you know the file is generated and can be re-retrieved. - Some entries depend on the org. A profile retrieved from a sandbox may list entries that exist only there. Do not merge them just because they appear.
If the profile is too tangled, abort and reconsider:
git merge --abortThis restores your branch to the state before the merge, so it is a safe way out.
Validate before you commit
A resolved file that looks right can still fail on deploy. Run the checks in this order:
# 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 commitThe grep catches leftover markers. xmllint catches broken tags. The dry run catches Salesforce-level problems, such as a permission that refers to a field the target org does not have.
The resolution workflow
Common errors
- Leaving one marker line behind. The XML becomes invalid, and
xmllintreports it. - Deleting a closing tag along with a marker. Select whole blocks, not lines.
- Keeping both sides of a competing edit. This creates duplicates.
- Resolving with
--oursor--theirson a hand-edited file. You silently lose the other branch's work. - Skipping validation because the merge "looks fine". Run the dry run every time.
Exercise
Main added <field>Account.Tier__c</field> with editable set to false. Your feature branch added the same field with editable set to true, and Git shows a conflict. Another conflict in the same file shows main adding Account.Region__c and your branch adding Account.Segment__c. How do you resolve each conflict, and which commands verify the result before you commit?
Show answer
For the Tier__c conflict, both sides changed the same element, so keep only one block. Confirm with the authors which editable value is intended. For the Region__c and Segment__c conflict, the fields differ, so keep both blocks and remove the marker lines. Then run grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-app to confirm no markers remain, run xmllint --noout on the file, check for duplicate <field> values, run sf project deploy start --dry-run --source-dir force-app --target-org my-sandbox, and finally git add the file and git commit.