Sub topic 12
Merge tools, rerere and .gitattributes
This chapter assumes you can already resolve a conflict by hand in a Salesforce DX project, as the earlier chapters showed. Here you set up three Git features that cut the repeated work: a visual merge tool, rerere, and .gitattributes.
Using a merge tool
A merge tool opens a conflicted file in a three-way view. You see your side, the other branch's side, and the common ancestor. This helps with long permission set and profile files, where markers are easy to misread.
VS Code can act as the tool. Add this to your Git configuration:
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
git config --global mergetool.keepBackup false
git mergetoolgit mergetool opens each conflicted file in turn. The keepBackup false setting stops Git from leaving .orig files in force-app. Those files are not valid metadata, and they can be deployed by accident if you stage them.
A merge tool does not know XML rules. After you save, run the validity checks from the earlier chapters.
Recording resolutions with rerere
rerere stands for "reuse recorded resolution". When you resolve a conflict, Git stores the conflicted text and your result. If the same conflict appears again, Git applies your earlier result.
This matters in Salesforce projects. Long-lived feature branches are often rebased or re-merged many times, and each pass can hit the same conflict in Sales_Access.permissionset-meta.xml.
git config --global rerere.enabled true
git merge feature/new-field
# Resolved 'force-app/.../Sales_Access.permissionset-meta.xml' using previous resolution.
git rerere status
git rerere diff
git add force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlrerere edits the file but does not stage it. Review the result with git rerere diff, then run git add yourself. If a recorded resolution was wrong, run git rerere forget <path> while the file is conflicted again. Then resolve it properly.
Setting merge behavior in .gitattributes
A .gitattributes file in the repository root sets a merge strategy for matching paths. Because it is committed, the whole team gets the same behavior.
# Keep lines from both sides, with no conflict markers
*.labels-meta.xml merge=union
# Never auto-merge generated files; always report a conflict
*.layout-meta.xml -mergemerge=unionis a built-in driver. It keeps the lines from both sides and writes no markers.-mergemakes Git treat the file as unmergeable. Any change on both sides is reported as a conflict, and Git does not attempt a line-level merge.
Do not use a driver that always takes one side, such as a custom ours driver, on metadata that people edit by hand. It discards the other branch's work without any warning.
Choosing the right tool
| Situation | Tool |
|---|---|
| Large file, hard to read markers | `git mergetool` |
| Same conflict returns on each rebase | `rerere` |
| Additive-only files such as custom labels | `merge=union`, with validation |
| Generated files that must be reviewed | `-merge` |
Exercise
A teammate adds merge=union for *.permissionset-meta.xml to stop conflicts. A merge then succeeds with no conflict, but the deployment dry run fails. What likely happened, and what should the team do?
Show answer
The union driver merged the two sides line by line without understanding XML. It probably kept two edits to the same element, or placed lines so the XML is invalid or contains duplicate entries. Permission sets are edited at the element level, so they are not safe for union merging. The team should remove the attribute for those files. They should resolve conflicts with a merge tool and then validate with xmllint and a dry run, and use rerere for repeated conflicts.