DaleSchool

Resolving Conflicts

Beginner25min

Learning Objectives

  • Describe at least two situations that cause conflicts
  • Read conflict markers and resolve them manually
  • Complete a merge after resolving conflicts
  • Build habits that prevent conflicts

Working Code

Example 1: Creating a conflict scenario

git init
echo "Project v1.0" > version.txt
git add version.txt
git commit -m "feat: add initial version file"

Modify on the feature branch:

git checkout -b feature-update
echo "Project v2.0 (feature)" > version.txt
git add version.txt
git commit -m "feat: update version to 2.0"

Modify the same file on main:

git checkout main
echo "Project v1.1 (main)" > version.txt
git add version.txt
git commit -m "fix: patch to version 1.1"

Now attempt the merge:

git merge feature-update

Output:

CONFLICT (content): Merge conflict in version.txt
Automatic merge failed; fix conflicts and then commit the result.

Example 2: Inspecting the conflict file

cat version.txt

Output:

<<<<<<< HEAD
Project v1.1 (main)
=======
Project v2.0 (feature)
>>>>>>> feature-update

Reading the conflict markers:

<<<<<<< HEAD          <- Current branch (main) content starts
Project v1.1 (main)
=======               <- Divider
Project v2.0 (feature)
>>>>>>> feature-update <- Incoming branch content ends

Example 3: Resolving the conflict

Open the file in an editor and replace the content with what you want:

# Remove markers and choose the desired content
echo "Project v2.0" > version.txt

# Mark as resolved
git add version.txt

# Complete the merge
git commit -m "merge: merge feature-update, consolidate to version 2.0"

Try It Yourself

Scenario 2: Multiple section conflicts

In real projects, conflicts can span multiple sections of a file:

git init
cat > README.md << 'EOF'
# Project
## Introduction
Introduction text here

## Installation
Installation instructions here
EOF
git add README.md
git commit -m "docs: add README"

# Modify both sections on feature
git checkout -b feature-docs
cat > README.md << 'EOF'
# Awesome Project
## Introduction
This project is truly awesome.

## Installation
Run npm install.
EOF
git add README.md
git commit -m "docs: flesh out README"

# Modify intro on main
git checkout main
cat > README.md << 'EOF'
# Project v2
## Introduction
This is the new version of the project.

## Installation
Installation instructions here
EOF
git add README.md
git commit -m "docs: add version info"

git merge feature-docs

Conflict file:

<<<<<<< HEAD
# Project v2
## Introduction
This is the new version of the project.

## Installation
Installation instructions here
=======
# Awesome Project
## Introduction
This project is truly awesome.

## Installation
Run npm install.
>>>>>>> feature-docs

Combine both versions:

cat > README.md << 'EOF'
# Awesome Project v2
## Introduction
This is the new version of the project, and it's truly awesome.

## Installation
Run npm install.
EOF

git add README.md
git commit

Scenario 3: Delete vs modify conflict

git init
echo "File to be deleted" > old-file.txt
git add old-file.txt
git commit -m "add: temporary file"

git checkout -b cleanup
git rm old-file.txt
git commit -m "chore: remove unnecessary file"

git checkout main
echo "Modified file" >> old-file.txt
git add old-file.txt
git commit -m "fix: modify file"

git merge cleanup

Output:

CONFLICT (modify/delete): old-file.txt deleted in cleanup and modified in HEAD.

Resolution options:

git status
# "both modified" shown

# Option A: Choose deletion
git rm old-file.txt
git commit -m "merge: resolve by deleting file"

# Option B: Keep the file
git add old-file.txt
git commit -m "merge: resolve by keeping modified file"

"Why?" -- When do conflicts happen?

Conflicts occur when:

  • Two branches modify the same line of the same file differently
  • One branch modifies a file while the other deletes it

Git auto-resolves when:

  • Different files are modified
  • The same file is modified on different lines

Three conflict resolution strategies

<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-branch
  1. Keep current branch: Delete everything below =====, keep HEAD content
  2. Keep incoming branch: Delete everything above =====, keep feature content
  3. Combine both: Remove only the markers, keep or merge both contents

Important: You must remove all marker lines (<<<, ===, >>>). Leaving markers in the file will break it.

Common Mistakes

Mistake 1: Leaving conflict markers in the file

# Build/runtime errors if markers remain
cat version.txt
# <<<<<<< HEAD        <- Marker left behind!
# v1.0
# =======
# v2.0
# >>>>>>> feature

Use git diff to verify all markers are gone.

Mistake 2: Committing before git add

# Wrong order
git commit   # Conflict not yet resolved -> error

# Correct order
# 1. Edit the conflict file
# 2. git add filename   (marks as resolved)
# 3. git commit

Mistake 3: Forgetting which branch is which

# Check current state during merge
git status
# Both modified: version.txt

# Check which branch HEAD points to
git branch
# * main  <- HEAD = main

HEAD is the current branch (main), and the named branch after >>>>>>> is the one being merged in.

Mistake 4: Doing other work mid-conflict

git merge feature-login   # Conflict occurs
# Haven't resolved yet

git checkout develop      # Error!
# error: Your local changes to the following files would be overwritten...

Either resolve the conflict fully or run git merge --abort to cancel before doing anything else.

Deep Dive

Resolving conflicts in VS Code

VS Code displays conflicts visually with one-click resolution buttons:

  • Accept Current Change -- Keep HEAD (current branch) content
  • Accept Incoming Change -- Keep incoming branch content
  • Accept Both Changes -- Keep both (in order)
  • Compare Changes -- Side-by-side comparison

Conflicted files show a ! icon in the Explorer.

Conflict prevention habits

You can't eliminate conflicts entirely, but you can reduce them:

  1. Pull/merge frequently: Regularly bring main changes into your feature branch

    git fetch origin
    git merge origin/main   # Run from the feature branch
    
  2. Keep PRs small: Large PRs have higher conflict potential

  3. Separate files: Structure your project so multiple people aren't editing the same file

  4. Communicate: Coordinate with teammates when working on the same file

Using git mergetool

For complex conflicts, a dedicated merge tool helps:

# Set VS Code as merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'

# Run (while in conflict state)
git mergetool

VS Code opens a 3-way merge view (base, current, incoming).

  1. Create a new repository with one file and commit it.
  2. Modify the same line on both a feature branch and main.
  3. Attempt a merge to trigger a conflict and inspect the markers with cat.
  4. Resolve the conflict in an editor, then run git add + git commit to complete the merge.
  5. Verify the merge commit with git log --oneline --graph.

Q1. After resolving a conflict, what is the correct command sequence to complete the merge?

  • A) git commit -> git add filename
  • B) git add filename -> git commit
  • C) git merge --continue alone is enough
  • D) git push -> git commit