debugModeratepending
Debug: Git merge conflict resolution strategies
Viewed 0 times
merge conflictconflict resolutioncheckout ourscheckout theirsmergetool
Error Messages
Problem
Git merge conflicts are confusing and risky to resolve, especially in complex files or with many conflicting changes.
Solution
Git merge conflict resolution:
For complex conflicts:
# 1. Understanding conflict markers
<<<<<<< HEAD (or current branch)
Your changes
=======
Their changes
>>>>>>> feature-branch (or incoming branch)
# 2. See what changed on each side
git diff --merge # Show only conflicting changes
git log --merge -p -- path/to/conflicted/file
# Shows commits from both sides that touch the file
# 3. Choose a resolution strategy BEFORE resolving
# Take ours (current branch wins)
git checkout --ours path/to/file
git add path/to/file
# Take theirs (incoming branch wins)
git checkout --theirs path/to/file
git add path/to/file
# For entire merge:
git merge feature -X ours # Prefer current on conflicts
git merge feature -X theirs # Prefer incoming on conflicts
# 4. Three-way merge in editor (recommended)
git mergetool # Opens configured merge tool
# Shows: BASE (common ancestor), LOCAL (yours), REMOTE (theirs)
# 5. After resolving
git add resolved-file.txt
git merge --continue
# Or: git commit (if no more conflicts)
# 6. Abort if it gets messy
git merge --abort # Undo the entire merge attempt
# 7. Prevent future conflicts
# - Merge main into feature branch frequently
# - Keep changes small and focused
# - Avoid reformatting files you're not otherwise changing
# - Use .editorconfig for consistent formatting
# 8. Check for remaining conflict markers
grep -rn '<<<<<<' . # Should return nothing
grep -rn '======' . # Double-check
grep -rn '>>>>>>' . # Triple-checkFor complex conflicts:
- Understand WHAT both sides intended, not just the diff
- Talk to the other developer if unsure
- Test thoroughly after resolution
- Consider cherry-picking specific changes instead
Why
Merge conflicts happen when two branches modify the same lines. Understanding the three-way merge (base, ours, theirs) makes resolution much easier than guessing from conflict markers alone.
Context
Git merge and collaboration
Revisions (0)
No revisions yet.