G
GuideDevOps
Lesson 7 of 18

Resolving Merge Conflicts

Part of the Git & Version Control tutorial series.

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
MarkerMeaning
<<<<<<<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

  1. Open conflicted file
  2. Find conflict markers
  3. Edit to keep desired code
  4. 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 message

Conflict 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 --continue

Accept Other Branch

# Keep all changes from merging branch
git checkout --theirs conflicted-file.ts
git add conflicted-file.ts
 
# Continue merge
git merge --continue

Abort Merge

git merge --abort
# Returns to state before merge started

Using Merge Tools

Built-in Merge Helper

# Get summary of conflicts
git diff --name-only --diff-filter=U
 
# See conflicts in context
git diff

Visual 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 resolution

Common 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 conflict

Both 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 correct

Workflow: 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 push

Preventing Conflicts

Communicate Early

# Keep branches short-lived
git branch -vv  # See age of branches

Rebase 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 branch

Code 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-lease

Advanced 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/auth

Rerere: Remember Resolutions

# Enable rerere (reuse recorded resolution)
git config --global rerere.enabled true
 
# Git remembers how you resolved conflicts
# Automatically applies same resolution next time

Revert Merge

# If merged wrong way:
git revert -m 1 <merge-commit-hash>
 
# Creates commit that undoes the merge

Tips

✓ 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

  1. Git will pause the merge and mark the files.
  2. Open the file. You will see markers like this: ``` HEAD My local change

    Someone else's change feature-branch ```
  3. Edit the file to keep the version you want (or a mix of both).
  4. Remove the markers.
  5. 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.