A Git merge conflict occurs when Git cannot safely combine competing changes. Git pauses the merge, marks the affected paths, and waits for you to choose the final content.
Why Merge Conflicts Happen
Conflicts usually appear when two branches change the same lines, one branch deletes a file that another branch edits, or files are renamed in incompatible ways. A conflict is not lost work; it is a decision Git cannot make for you.
Prepare Before Merging
Commit or stash unrelated work, update your remote references, and switch to the branch that should receive the changes:
# Confirm that the working tree is clean
git status --short
# Update remote-tracking branches
git fetch origin
# Merge the feature into main
git switch main
git merge feature/cart-summary
Example conflict output:
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
Find Conflicted Files
Use status for guidance:
git status
Example output:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
both modified: index.html
List only unresolved paths when the repository contains many changes:
git diff --name-only --diff-filter=U
Example output:
index.html
Read Conflict Markers
For a text conflict, Git writes markers into the working-tree file:
Conflicted file:
<<<<<<< HEAD
<p>Cart total: $48</p>
=======
<p>Order total: $48</p>
>>>>>>> feature/cart-summary
<<<<<<< HEADstarts the current branch's version.=======separates the two versions.>>>>>>> feature/cart-summaryends the incoming version.
Edit the file so it contains the intended result and remove every marker.
Resolved file:
<p>Order total: $48</p>
Review the Resolution
Check the changes before staging them:
git diff
You can also search again for unresolved paths:
git diff --name-only --diff-filter=U
If the second command prints nothing, Git has no unmerged index entries left.
Stage the Resolved Files
git add index.html
git status --short
Example output:
M index.html
The staged M shows that the resolved version is ready for the merge commit.
Complete the Merge
After resolving and staging every conflict, continue the merge:
git merge --continue
Git opens the configured editor for the merge commit message. You can also run git commit directly. A successful completion may show:
Example output:
[main 81ac409] Merge branch 'feature/cart-summary'
Abort or Quit the Merge
Return to the pre-merge state when you do not want to finish:
git merge --abort
Git may be unable to reconstruct unrelated uncommitted changes, which is why a clean working tree is important before merging.
git merge --quit forgets the merge operation but leaves the index and working tree unchanged. Use it only when you intend to handle that state yourself.
Resolve Rebase and Cherry-Pick Conflicts
The file-resolution steps are the same, but the continuation command must match the operation:
| Operation | Continue | Abort |
|---|---|---|
| Merge | git merge --continue | git merge --abort |
| Rebase | git rebase --continue | git rebase --abort |
| Cherry-pick | git cherry-pick --continue | git cherry-pick --abort |
Use a Merge Tool
Launch a configured visual merge tool when side-by-side comparison helps:
git mergetool
Review the final files and status even when a tool reports success.
Reduce Future Conflicts
- Keep commits focused and merge long-lived branches regularly.
- Coordinate before several people edit the same section.
- Run tests after resolving code conflicts.
- Inspect staged changes before completing the merge.
Conclusion
Resolve conflicts by locating unmerged paths, editing the final content, reviewing the diff, staging the resolution, and continuing the operation that caused the conflict. Always verify status and tests before sharing the result.