Sub topic 1
How Git detects and reports merge conflicts
This chapter explains when Git stops a merge, what it prints, and how to find the files you must fix. Later chapters cover reading the markers and resolving them.
Prerequisites
This chapter assumes you have the following:
- Basic Git use: branches, commits, merge and pull.
- A Salesforce DX project with a
force-appdirectory in source format. - A basic understanding of XML structure.
- The Salesforce CLI (
sf) and access to a sandbox or scratch org.
How Git decides there is a conflict
A merge combines two lines of history. Git finds the merge base, which is the most recent commit both branches share. It then compares three versions of each file: the base, your branch (often called "ours"), and the branch you are merging in ("theirs").
Git applies these rules to each file:
- If only one side changed a region, Git takes that change.
- If both sides made the identical change, Git takes it once.
- If both sides changed the same region, or changed lines that touch each other, in different ways, Git cannot choose. It reports a conflict.
Git works on lines of text. It does not know that a file is XML, so it cannot tell that two fieldPermissions blocks are separate elements. In a metadata file, two people who add different elements at the same position still change the same lines, and Git flags the overlap.
What a conflicted merge looks like
Git merges every file it can and stops when it reaches one it cannot combine. It prints the file names and leaves the merge unfinished.
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 output has three parts:
Auto-mergingmeans Git is combining both sides of that file.CONFLICT (content)means both sides changed the same region of that file.Automatic merge failedmeans no merge commit was made. Your repository is now in a merging state.
Files that merged cleanly are already staged. Conflicted files are not. Git writes the markers <<<<<<<, ======= and >>>>>>> into them.
Listing the files you must fix
Two commands show the unresolved files:
git statuslists them under "Unmerged paths", with the statusboth modified.git diff --name-only --diff-filter=Uprints only the unmerged paths, one per line. TheUfilter means "unmerged". This output is easy to pass to other tools.
A conflict is not always in the file contents. Git can also report modify/delete (one side edited a file the other deleted) and rename/rename (both sides renamed a file differently). These appear in git status with other labels, such as deleted by us or deleted by them.
Leaving a merge safely
The merge is in progress until you commit. If the situation is unclear, you can return to the state before you ran git merge:
git merge --abortThis discards the merge attempt and keeps your earlier commits. Run it only if you have not made edits you want to keep, because it removes your partial resolution.
Exercise
You run git merge and Git prints "Automatic merge failed". Which command prints only the paths of files with unresolved conflicts, and what does the U in its filter mean?
Show answer
git diff --name-only --diff-filter=U prints only the paths. The U stands for "unmerged", the files that still have unresolved conflicts.
Exercise
Two developers each add a different fieldPermissions block to the same permission set at the same position. Why does Git report a conflict even though the XML elements are independent?
Show answer
Git compares lines, not XML elements. Both sides changed the same region relative to the base, so Git cannot tell the changes are independent and marks a conflict.