Sub topic 12
Merge tools, rerere and .gitattributes
This chapter assumes you can already read conflict markers in metadata XML, resolve a conflict by hand, and validate the result with a deployment dry run. It covers three Git features that reduce the manual work: a merge tool with a three-way view, rerere for repeated conflicts, and .gitattributes for per-file merge rules. None of them replaces validation.
Make conflicts easier to read
Git's default conflict style shows only your side and their side. For metadata XML you often need the common ancestor too, so you can see what each branch changed.
git config --global merge.conflictstyle diff3With diff3, each conflict gains a middle section that starts with ||||||| and holds the base version:
<<<<<<< HEAD
<editable>false</editable>
||||||| merged common ancestors
<editable>true</editable>
=======
<editable>true</editable>
>>>>>>> feature/new-fieldHere the base had true, your branch changed it to false, and the other branch left it alone. That is not a real disagreement, so you keep false. Without the base section you cannot tell this case from a true clash.
Git 2.35 and later also offer zdiff3. It works like diff3 but moves lines common to both sides out of the conflict, which makes blocks smaller. Both styles add the ||||||| line, so any script that searches for markers must match it too.
Use a graphical merge tool
git mergetool opens each conflicted file in an external tool, usually with a local, remote, base and result pane. This helps most with large profiles and permission sets, where many conflicts are scattered through a long file.
Configure Visual Studio Code as the tool:
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge "$REMOTE" "$LOCAL" "$BASE" "$MERGED"'
git config --global mergetool.keepBackup falseThen run it after a conflicted merge:
git merge feature/new-field
git mergetoolGit opens each unresolved file in turn. When you save and close, Git marks that file as resolved and moves to the next one.
keepBackup falsestops Git from leaving.origfiles next to your metadata. Those stray files are not valid source format, and a deployment can pick them up if they sit underforce-app.- A merge tool resolves text, not XML structure. It lets you accept a line from either side, so you can still produce a mismatched tag. Run
xmllint --nooutafterward, as in the earlier chapters. - Tools with a "both sides" action are convenient for additive changes. They still do not know that
<fieldPermissions>blocks must stay sorted or unique.
Reuse resolutions with rerere
rerere stands for "reuse recorded resolution". When you resolve a conflict, Git stores the conflicted hunk and your resolution. If the identical conflict appears again, Git applies the stored resolution for you.
Enable it once:
git config --global rerere.enabled trueSalesforce teams hit repeated conflicts in several common situations:
- A long-lived feature branch that you merge
maininto several times. The same profile hunk conflicts each time. - A rebase of a branch with many commits touching one permission set. Each commit may replay the same conflict.
- A trial merge that you abort with
git merge --abort, then redo for real after other work lands. - A release branch and a hotfix branch that both receive the same change.
After you resolve and commit, Git has recorded the resolution. The next time the same conflict appears, Git reports it:
Resolved 'force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml' using previous resolution.By default Git applies the resolution to the working file but does not stage it. Review the file, then stage it yourself. To stage reused resolutions automatically, set rerere.autoupdate true. I do not recommend that for metadata, because you lose the chance to look at the file first.
Useful commands:
| Command | What it does |
|---|---|
| `git rerere status` | Lists files whose conflicts Git is tracking in the current merge. |
| `git rerere diff` | Shows the resolution Git has recorded so far for those files. |
| `git rerere forget -- <path>` | Discards the recorded resolution for one file. |
| `git rerere gc` | Removes old records. |
What to watch for
- The record is local. Git stores it in
.git/rr-cacheinside your clone. It is not pushed, so each teammate and each CI runner builds its own record. - A bad resolution is reused as faithfully as a good one. If you once recorded a resolution that left invalid XML, rerere will apply it again. Run
git rerere forget -- <path>and resolve the file again. - Matching is exact. Git keys a record on the conflicted text. If someone adds one more
fieldPermissionsblock inside the conflict, it is a new conflict. - It does not replace validation. A reused resolution that was correct against an older base can be wrong now. Run the dry run each time.
Set per-file merge rules with .gitattributes
A .gitattributes file at the project root tells Git how to treat paths. It is committed, so every clone shares it. Three uses matter for Salesforce projects.
Normalize line endings
Windows and macOS teammates can introduce line-ending changes that make a whole file look different. That turns a small edit into a large conflict.
# .gitattributes
* text=auto
*.xml text eol=lf
*.cls text eol=lf
*.trigger text eol=lf
*.js text eol=lf
*.png binary
*.jpg binaryIf your repository already contains mixed endings, normalize in a separate commit before you merge other work. Mixing a normalization commit with real changes makes the review hard.
Mark binary files
binary stops Git from trying a text merge on images and similar files. Git then reports a conflict and leaves you to choose one version with git checkout --ours or --theirs. This is safer than letting it merge bytes.
Assign a merge driver
The merge attribute names a driver for matching paths. Git has a built-in union driver that keeps lines from both sides and never reports a conflict.
force-app/main/default/labels/CustomLabels.labels-meta.xml merge=unionThis is tempting for additive files, but it is risky for XML. It joins lines from both sides without understanding structure. Two edits to the same element can leave duplicate lines or a half-edited block. Two branches that add the same label name produce a duplicate that the deployment rejects. Use union only where you have proof it is safe, and validate every merge.
For files that are generated and easy to regenerate, define your own driver. The attribute goes in .gitattributes, and the driver definition goes in Git config:
# .gitattributes
force-app/main/default/layouts/*.layout-meta.xml merge=theirs-regengit config merge.theirs-regen.name "Take the incoming version"
git config merge.theirs-regen.driver "cp %B %A"Git passes the base as %O, your version as %A, and their version as %B. The driver must write its result to %A and exit with status 0 for a clean merge. Here cp %B %A simply replaces your version with theirs.
Two limits apply to custom drivers:
- The driver definition lives in
.git/configor your global config, not in the repository. Every teammate must run thegit configlines, or Git ignores the attribute and uses the normal merge. - The driver resolves silently. You will see no conflict and no markers, so a change you made on your side is dropped without warning. Limit it to files you can re-retrieve from an org.
Choosing between them
| Situation | Best fit |
|---|---|
| Hard-to-read conflict in a large permission set | `diff3` or `zdiff3`, plus `git mergetool` |
| Same conflict returns on every merge of `main` | `rerere` |
| Line-ending noise across operating systems | `text eol=lf` in `.gitattributes` |
| Generated file that you can re-retrieve | Custom driver, or `git checkout --theirs` |
| Additive list in a file you have tested | `merge=union`, only with validation |
Exercise
A teammate adds merge=union for Sales_Access.permissionset-meta.xml. Two branches each edit the <editable> value of Account.Tier__c, one to true and one to false. What does the merge produce, and why is that a problem?
Show answer
The union driver keeps the lines from both sides and reports no conflict. The result can hold both <editable> lines in one fieldPermissions block, which is invalid for the metadata API or has ambiguous meaning. Nothing warns you. Remove the attribute for this file and resolve the conflict by hand.
Exercise
You resolved a conflict with rerere enabled, then noticed the stored resolution left a mismatched closing tag. How do you stop Git from reusing it?
Show answer
Run git rerere forget -- <path> for that file. Then resolve the conflict again, check it with xmllint --noout, and commit.
Exercise
Why does defining merge.theirs-regen.driver with git config not protect a teammate who clones the repository, even though .gitattributes is committed?
Show answer
The attribute is committed, but the driver definition is stored in local Git config, which is not shared. A clone without that config ignores the attribute and falls back to the normal merge, so the teammate sees conflicts that you do not.