Understanding and Resolving Git Merge Conflict Diffs
Learn how to read and resolve Git merge conflict markers in diff output. Understand three-way diffs, conflict resolution strategies, and how to use diff tools to resolve conflicts safely.
Detailed Explanation
Merge Conflict Diffs
Merge conflicts occur when Git cannot automatically reconcile changes from two branches. Understanding the conflict markers and using diff tools effectively is essential for safe conflict resolution.
Conflict Marker Anatomy
<<<<<<< HEAD
const API_URL = "https://api.staging.example.com";
=======
const API_URL = "https://api.production.example.com";
>>>>>>> feature/deploy-config
<<<<<<< HEAD— start of your current branch's version=======— separator between the two versions>>>>>>> feature/...— end, showing the incoming branch name
Three-Way Diff
A three-way diff shows three versions:
- Base — the common ancestor (before either branch changed the line)
- Ours — the current branch (HEAD)
- Theirs — the incoming branch
BASE: const API_URL = "https://api.example.com";
OURS: const API_URL = "https://api.staging.example.com";
THEIRS: const API_URL = "https://api.production.example.com";
Seeing the base version helps you understand the intent of each change.
Conflict Resolution Strategies
| Strategy | When to Use |
|---|---|
| Accept ours | Your branch's change is correct |
| Accept theirs | Incoming branch's change is correct |
| Accept both | Both changes are needed (different sections) |
| Manual merge | Need to combine logic from both versions |
Common Conflict Patterns
Both modified the same line:
<<<<<<< HEAD
timeout: 30,
=======
timeout: 60,
>>>>>>> feature/performance
Resolution: decide which timeout value is correct, or discuss with the other developer.
Adjacent line modifications:
<<<<<<< HEAD
host: "localhost",
port: 3000,
=======
host: "0.0.0.0",
port: 8080,
>>>>>>> feature/docker
Resolution: you might need the host from one branch and the port from another.
One branch deleted, another modified:
<<<<<<< HEAD
function legacyHandler() {
// updated implementation
}
=======
>>>>>>> feature/cleanup
Resolution: decide if the function should be kept (with updates) or deleted.
Best Practices
- Pull frequently — smaller diffs mean fewer conflicts
- Use a three-way merge tool — VS Code, IntelliJ, or
git mergetool - Run tests after resolving — ensure nothing is broken
- Review the final diff —
git diff --stagedbefore committing - Communicate — talk to the other developer if unsure about their changes
Use Case
Every developer encounters merge conflicts. Understanding conflict diffs is essential for maintaining code integrity during team collaboration. It is especially important in fast-moving projects with multiple developers working on related code, during release branch merges, and when rebasing long-lived feature branches onto an updated main branch.