Sub topic 13
Reducing future conflicts
This chapter assumes you can resolve a conflict by hand, run sf commands against a sandbox, and read Salesforce DX source format. The goal here is to have fewer conflicts to resolve. Most of the techniques below are cheap, and some are one-time configuration.
Why prevention works
Earlier chapters showed that conflicts in metadata come from a few repeatable causes: many people editing the same large XML file, files that get reordered when retrieved, and long-lived branches that drift from the main line. Each cause has a matching countermeasure.
| Cause | Countermeasure |
|---|---|
| Many developers edit one large file (profiles, permission sets) | Split access into smaller permission sets and assign ownership |
| Retrieved files change order or format without a real change | Retrieve only what you changed and commit small diffs |
| Branches live for weeks and drift | Short-lived branches and frequent merges from the main branch |
| Two people change the same component unaware of each other | Communication, ownership, and a visible work list |
| The same conflict recurs on every merge | `rerere` and `.gitattributes` |
| Bad resolutions reach the main branch | CI validation before merge |
Keep branches short and synchronized
A conflict grows with the distance between two branches. A branch that is two days old usually conflicts in a few lines. A branch that is six weeks old can conflict in dozens of files.
- Merge or rebase the main branch into your feature branch at least daily.
- Split large features into pieces that can merge on their own.
- Merge finished work promptly instead of waiting for a release.
Merging often turns one large conflict into several small ones. You resolve each while the changes are still fresh in your mind.
# Bring the latest main into your feature branch and resolve small conflicts early
git fetch origin
git merge origin/mainIf your team rebases instead of merging, the same rule applies. Rebase often, because each replayed commit can conflict on its own.
Make smaller, more focused commits
Commits that touch only the components you changed produce smaller diffs, and smaller diffs overlap less often.
A common source of noise is retrieving a whole object or profile when you changed one field. The retrieved file may differ from the committed one in ordering or in unrelated entries that other people changed in the org. Retrieve narrowly and review the diff before you commit.
# Retrieve only the component you changed
sf project retrieve start --metadata CustomField:Account.Segment__c --target-org my-sandbox
# Review exactly what changed before staging
git diff --stat
git diffStage with git add -p when a file contains both your change and unrelated retrieved changes. Commit only your hunks.
Move access out of profiles
Profiles are the largest source of conflicts. One profile file contains field permissions, object permissions, Apex class access, page layout assignments, and more, for every component in the org. Any two developers who add a field are likely to touch the same file.
Permission sets are structured the same way, but they are smaller and can be scoped to one feature. A practical approach:
- Keep profiles minimal. Use them for settings that only profiles can hold, such as login hours, and for the default layout assignments.
- Grant feature access through permission sets named for the feature, such as
Segmentation_Access. - Group related permission sets in a permission set group when users need several at once.
- Give each team a set it owns, so two teams rarely edit the same file.
With this layout, a developer who adds Account.Segment__c edits Segmentation_Access.permissionset-meta.xml. Someone working on a different feature edits a different file, and Git has nothing to conflict.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<fieldPermissions>
<editable>true</editable>
<field>Account.Segment__c</field>
<readable>true</readable>
</fieldPermissions>
<hasActivationRequired>false</hasActivationRequired>
<label>Segmentation Access</label>
</PermissionSet>Agree on ownership and communicate
Tooling cannot detect that two people plan to rebuild the same flow. A lightweight habit prevents most of these cases.
- Name an owner for each high-traffic component: the main permission sets, key flows, and shared layouts.
- Announce in the team channel or the ticket when you start work on a shared component.
- Use a shared sandbox or scratch org per developer. Editing the same org in the Setup UI is a common cause of changes that overwrite each other.
- Use CODEOWNERS in your Git host so that owners review changes to their files.
# .github/CODEOWNERS
/force-app/main/default/flows/ @example-org/automation-team
/force-app/main/default/permissionsets/ @example-org/security-teamLet Git remember and help with `rerere`
If you keep a long-running branch, you may resolve the same conflict repeatedly as you merge or rebase. The rerere feature (reuse recorded resolution) records how you resolved a conflict and applies the same fix when the same conflict appears again.
# Enable for the current repository
git config rerere.enabled true
# Or for every repository on your machine
git config --global rerere.enabled trueGit applies a recorded resolution but does not stage it for you. Run git diff to check the result, then git add the file. A recorded resolution can also be wrong, so validate it as you would a manual one.
Use `.gitattributes` for files you never merge by hand
.gitattributes tells Git how to treat paths. For Salesforce projects, two settings are useful.
First, line endings. A team with Windows and macOS developers can produce files that differ only in line endings, which creates conflicts that look like whole-file changes. Normalize them:
# .gitattributes
* text=auto eol=lf
*.xml text eol=lfSecond, generated files that you always re-retrieve. You can choose a merge strategy per path. The built-in union driver keeps lines from both sides. That suits line-based lists, but it can also produce invalid XML, so do not use it for metadata XML. For generated files where you always want your own version, define a custom driver:
git config merge.ours.driver true# .gitattributes
# Keep the current branch's version of this generated file during merges
package-lock.json merge=oursKeep source format and project settings consistent
- Use source format throughout. Files in the older metadata format are single large files, such as a whole custom object in one XML, and they conflict far more often.
- Do not commit local-only files. Keep
.sfdx/,.sf/, and personal settings in.gitignore. - Use
.forceignoreto exclude components that you do not manage in source control, so retrieves do not add noise. - Pin the Salesforce CLI version used by the team. Different CLI versions can serialize the same metadata differently, which produces diffs without real changes.
# Check which CLI version each developer and CI is using
sf --versionValidate before merging
Prevention also includes catching bad merges before they reach the main branch. A pull request check that runs a deployment dry run against a sandbox, plus a simple marker search, catches most errors from manual resolution.
#!/usr/bin/env bash
set -euo pipefail
# Fail if conflict markers were committed
if grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-app; then
echo "Conflict markers found" >&2
exit 1
fi
# Fail if the deployment would fail in the target org
sf project deploy start --dry-run --source-dir force-app --target-org ci-sandboxThe marker search is a quick filter. The dry run is the real check, because it compiles and validates the metadata with the org's rules.
How the practices fit together
Order of adoption
You do not need to adopt everything at once. Start with the changes that cost least and remove the most conflicts.
- Enable
rerereand add.gitattributesline-ending rules. This takes minutes. - Add the CI marker search and dry run.
- Adopt the daily sync habit and narrow retrieves.
- Introduce ownership and CODEOWNERS.
- Plan the move from profile-based to permission-set-based access. This is the largest change and carries the most risk.
Exercise
Your team has frequent conflicts in Sales_Access.permissionset-meta.xml and in one profile because every feature adds fields to them. Name three changes that reduce this, and say why you would not put merge=union on these files.
Show answer
Possible changes: create smaller permission sets per feature so developers edit different files; keep profiles minimal; merge the main branch into feature branches daily so conflicts stay small; assign an owner and use CODEOWNERS; enable rerere for recurring conflicts. merge=union keeps lines from both sides without understanding XML structure, so it can produce duplicated or invalid elements and can hide real competing edits, which breaks deployment or changes access without anyone noticing.
Exercise
A developer enabled rerere and a conflict was resolved automatically during a merge. What should they do before committing?
Show answer
Run git diff to review the applied resolution, check that no markers remain and the XML is well-formed, run a deployment dry run against a sandbox, then stage the file with git add and commit. Git applies the recorded resolution but does not stage it, and the earlier resolution may not fit the new context.