What is a Merge Conflict?
When Git can't automatically merge changes, it creates a conflict. This happens when:
- Both branches modified the same lines
- One branch deleted a file another modified
- Both branches added different changes in the same location
Understanding Conflicts
Conflict Markers
When conflict occurs, Git marks conflicted sections:
<<<<<<< HEAD
function login(user) {
validateEmail(user.email);
password = hash(user.password);
=======
function login(user) {
validate(user);
hashPassword(user);
>>>>>>> feature/auth
| Marker | Meaning |
|---|---|
<<<<<<< | Start of current branch (HEAD) |
======= | Separator between branches |
>>>>>>> | End of other branch |
Detecting Conflicts
git merge feature/auth
# If conflict:
# CONFLICT (content): Merge conflict in app.ts
# Automatic merge failed; fix conflicts and commit
git status
# Shows conflicted files, marked as "both modified"Resolving Conflicts
Manual Resolution
- Open conflicted file
- Find conflict markers
- Edit to keep desired code
- Remove conflict markers
// Original conflict
<<<<<<< HEAD
const delay = 1000;
=======
const delay = 5000;
>>>>>>> feature/async
// Resolved - keep both with logic:
const delay = isProduction ? 5000 : 1000;Mark as Resolved
# After editing
git add resolved-file.ts
# Continue merge
git merge --continue
# Usually opens editor for merge commit messageConflict Resolution Strategies
Accept Current Branch
# Keep all changes from current branch
git checkout --ours conflicted-file.ts
git add conflicted-file.ts
# Continue merge
git merge --continueAccept Other Branch
# Keep all changes from merging branch
git checkout --theirs conflicted-file.ts
git add conflicted-file.ts
# Continue merge
git merge --continueAbort Merge
git merge --abort
# Returns to state before merge startedUsing Merge Tools
Built-in Merge Helper
# Get summary of conflicts
git diff --name-only --diff-filter=U
# See conflicts in context
git diffVisual Merge Tool
# Configure merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait"
# Use merge tool
git mergetool
# Opens GUI for visual conflict resolutionCommon Tools
- VS Code: Built-in conflict resolution UI
- Vim/Neovim: Vimdiff
- GitKraken: Visual merge interface
Common Conflict Scenarios
Conflicting Line Edits
// Branch A modified line 5
const config = getConfig();
// Branch B modified same line
const config = loadConfig('default');
// Must choose or combine logic
const config = getConfig() || loadConfig('default');Deleted vs. Modified
# One branch deleted file, other modified it
# Option 1: Delete file
git rm deleted-file.ts
# Option 2: Keep file
git add kept-file.ts
# Then resolve the conflictBoth Added New Files
# Both branches added same filename with different content
# Usually no actual conflict - both files exist
git status
# Shows file, resolve by choosing which one is correctWorkflow: Resolving Complex Merge
# Try merge
git merge feature/large-refactor
# Conflicts occurred
git status
# See conflicts
git diff
# Open editor and manually resolve
# Use IDE's merge conflict UI
# After fixing each file:
git add resolved-file.ts
git add another-file.ts
# Complete merge
git commit -m "Merge feature/large-refactor"
# Push
git pushPreventing Conflicts
Communicate Early
# Keep branches short-lived
git branch -vv # See age of branchesRebase Before Merge
# Get latest main changes
git fetch origin
git rebase origin/main
# May have conflicts to resolve before merging
# Better to find them early in your branchCode Review
Pull requests catch conflicts before merge:
# Create PR with your branch
# GitHub/GitLab shows conflicts if any
# Resolve locally, force push
git push origin --force-with-leaseAdvanced Conflict Resolution
Merge with Strategy
# Use "ours" strategy (prefer current branch)
git merge -X ours feature/auth
# Use "theirs" strategy (prefer merging branch)
git merge -X theirs feature/authRerere: Remember Resolutions
# Enable rerere (reuse recorded resolution)
git config --global rerere.enabled true
# Git remembers how you resolved conflicts
# Automatically applies same resolution next timeRevert Merge
# If merged wrong way:
git revert -m 1 <merge-commit-hash>
# Creates commit that undoes the mergeTips
✓ Pull latest before starting new feature
✓ Keep branches focused to minimize conflicts
✓ Communicate with team about areas being modified
✓ Use merge tools for visual resolution
✓ Test thoroughly after resolving conflicts
✓ Understand the context - don't just "keep both"
✓ Rerere for repeated conflict patterns
title: "Resolving Merge Conflicts" description: "What to do when two people change the same line of code." order: 7
What is a Conflict?
A merge conflict happens when Git doesn't know which change to keep because the same line was modified in both branches.
How to Resolve
- Git will pause the merge and mark the files.
-
Open the file. You will see markers like this: ``` HEAD My local change
Someone else's change feature-branch ``` - Edit the file to keep the version you want (or a mix of both).
- Remove the markers.
- Save, then: ```bash git add git commit -m "fix: resolve merge conflict" ```
Warning: Never force merge! Always communicate with the author of the conflicting change before deciding which code to delete.