Sub topic 1
How Git detects and reports merge conflicts
Before you can fix a conflict in a Salesforce metadata file, you need to know what Git means by a conflict and where it keeps the evidence. This chapter covers how Git decides that a merge cannot finish on its own. It also covers the messages and commands that tell you which files need attention. Later chapters cover reading the markers inside a file and resolving them.
Prerequisites
This chapter assumes you already have the following:
- Basic Git use: you can create branches, commit, and run
git mergeandgit pull. - A Salesforce DX project with a
force-appdirectory in source format. - A rough idea of how XML elements nest.
- The Salesforce CLI (
sf) installed, so you can later validate against a sandbox or scratch org.
What a merge actually compares
Git does not compare your branch to the other branch directly. It performs a three-way merge, which uses three versions of each file:
- The merge base is the most recent commit that both branches share. It is the common ancestor.
- "Ours" is the tip of the branch you are on, usually called
HEADduring a merge. - "Theirs" is the tip of the branch you are merging in.
You can ask Git for the merge base yourself:
git merge-base HEAD feature/new-fieldGit then asks one question per changed region: what did each side do relative to the base?
- If only one side changed a region, Git takes that change.
- If both sides changed it in exactly the same way, Git takes that single result.
- If both sides changed the same region in different ways, Git cannot choose. This is a conflict.
Git knows nothing about Salesforce. It sees lines of text, not fieldPermissions elements. It does not know that two permission blocks are independent of each other. It only knows whether the line ranges changed on each side overlap or touch.
When Git reports a conflict and when it does not
The table shows common situations in a metadata project. Assume each branch edits Sales_Access.permissionset-meta.xml.
| Situation | Result | Reason |
|---|---|---|
| Branch A edits the top of the file, branch B edits the bottom | Merges cleanly | The changed regions are far apart |
| Both branches add a different block at the same position | Conflict | Both inserted text at the same spot in the base |
| Both branches add the identical block | Merges cleanly | Both sides produced the same text |
| One branch edits a block, the other deletes it | Conflict | One side changed the lines and the other removed them |
| One branch renames the file, the other edits it | Usually merges cleanly | Git follows the rename and applies the edit |
| Both branches edit a binary file such as a static resource ZIP | Conflict | Git cannot merge binary content line by line |
The last point on clean merges deserves a warning. A merge that Git reports as clean can still produce a bad file. Two edits in different places can combine into a result that is syntactically valid but wrong. For example, one branch removes a field permission and another branch references that field. Git checks text overlap, not meaning. This is why the deployment dry run in a later chapter still matters after a clean merge.
Seeing the conflict report
Suppose feature/new-field and your current branch each added a field permission to the same permission set. Running the merge stops partway 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.Read the three lines in order:
Auto-mergingmeans Git tried to combine the file itself.CONFLICT (content)means the failure was in the file's contents. Other types exist, such as modify/delete or rename/rename.Automatic merge failedmeans Git applied everything it could and stopped before creating the merge commit.
The merge is now in progress. Your working tree holds a mixture: files that merged cleanly are already staged, and conflicted files contain markers. Git does not discard anything, and nothing is committed yet.
Finding the conflicted files
Run git status first:
git status
# On branch main
# You have unmerged paths.
# (fix conflicts and run "git commit")
# (use "git merge --abort" to abort the merge)
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
# both modified: force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlThe Unmerged paths section lists only the files that still need a decision. The label before each path tells you the kind of conflict. both modified means both sides changed the file. deleted by us or deleted by them means one side removed a file the other side edited. both added means each side created a file at the same path.
In a large project, git status output can be long. This command prints only the paths of unresolved files, which is easy to feed into other commands:
git diff --name-only --diff-filter=UThe filter U stands for unmerged. After you stage a file with git add, it disappears from this list, so the list shrinks as you work.
Where Git keeps the three versions
While a merge is in progress, Git's index (the staging area) stores up to three copies of each conflicted file. These are called stages:
| Stage | Version | Shortcut for `git show` |
|---|---|---|
| 1 | Merge base (common ancestor) | `:1:path` |
| 2 | Ours (`HEAD`) | `:2:path` |
| 3 | Theirs (the branch being merged) | `:3:path` |
A file that is not in conflict has only stage 0. List the staged entries for conflicted files like this:
git ls-files -u
# 100644 3f1c9a7e2b0d4c5a8e6f1a2b3c4d5e6f7a8b9c0d 1 force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# 100644 8a2d4e6f0b1c3d5e7f9a1b3c5d7e9f0a2b4c6d8e 2 force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# 100644 c7e5a3b1d9f7e5c3a1b9d7f5e3c1a9b7d5f3e1c9 3 force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlThe object hashes above are examples, and yours will differ. The number before the tab is the stage. The three rows for one path confirm a real content conflict.
You can read any stage directly, even after you have edited the working copy. This is useful when you have started editing and want to see what the original looked like:
git show :1:force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git show :2:force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git show :3:force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlComparing the sides
Git has several ways to look at a conflict without opening the file. By default, git diff during a merge shows a combined diff of the conflicted regions. You can also compare the working file against one specific stage:
git diff --base -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git diff --ours -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git diff --theirs -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlTo see which commits on each side touched the conflicted files, use:
git log --merge --oneline -- force-appThe --merge option limits the log to commits that changed the conflicted paths on either side. In a Salesforce project this often tells you who edited a permission set and which pull request they were working on, so you know whom to ask about intent.
How a merge moves through its states
During this time Git records that a merge is underway by creating the file .git/MERGE_HEAD, which holds the commit you are merging in. When you run git commit and the merge finishes, Git uses that file to give the commit two parents. It then removes the file.
Rules that surprise people
- Staging is the signal. Git considers a file resolved when you run
git addon it. It does not read the file to check that the markers are gone. If you stage a file that still contains<<<<<<<lines, Git accepts it, and that text goes into your commit. - Git blocks the commit only for unmerged paths.
git commitrefuses to run while any path is still unmerged, but it does nothing about leftover markers in files you staged. - Aborting is safe if you had no uncommitted work.
git merge --abortrestores the state from before the merge. If you had uncommitted changes when you started, Git may not be able to rebuild them, so commit or stash first. - A
git pullcan start a merge. By defaultgit pullruns a fetch and then a merge, so you can land in this state without typinggit merge. - Whitespace counts. Salesforce tooling can reformat XML indentation or line endings. Git treats those as real changes, which can create conflicts where the content is the same. Later chapters address this.
What to check next
By now you can tell that a conflict exists, which files are involved, and what each side contributed. The next chapters look at the Salesforce DX source format and at why metadata files conflict so often. After that, you read the markers in the file and resolve them.
Exercise
You run git merge feature/new-field and Git reports a conflict in one permission set. You open git ls-files -u and see three rows for that file with stages 1, 2 and 3. What does each stage hold, and how would you print your own branch's version of the file without opening the working copy?
Show answer
Stage 1 is the merge base, the common ancestor of both branches. Stage 2 is "ours", the version on the branch you are on (HEAD). Stage 3 is "theirs", the version from feature/new-field. To print your branch's version, run git show :2:force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml.
Exercise
A teammate merges two branches. Git reports no conflicts. One branch deleted a fieldPermissions block for Account.Tier__c, and the other branch added a new custom field that depends on that permission. Why can the deployment still fail, and what check catches it?
Show answer
Git only checks whether the edited text regions overlap, and these edits were in different places, so it merged cleanly. It has no knowledge of what the metadata means. The combined result can still be rejected by Salesforce. A deployment dry run against a sandbox, for example sf project deploy start --dry-run --source-dir force-app --target-org my-sandbox, catches this before the merge is committed or pushed.