Resolving Merge Conflicts

Resolving Merge Conflicts

Conflict Resolution

Introduction

Merge conflicts occur when Git cannot automatically combine changes. This lesson teaches you how to identify and resolve conflicts.

What Causes Conflicts?

Conflicts happen when:

  • Same line modified in different branches
  • A file deleted in one branch, modified in another
  • Same file renamed differently

Identifying Conflicts

During Merge

When conflicts occur, Git will show:

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

Check Status

git status

Shows files with conflicts:

Unmerged paths:
  (both modified):   file.txt

View Conflict Markers

Open conflicting file:

<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-branch

Resolving Conflicts

Method 1: Manual Edit

1. Open the file 2. Remove conflict markers 3. Keep desired content 4. Save the file 5. Stage and commit

# Edit file manually
nano file.txt

Stage the resolved file

git add file.txt

Complete the merge

git commit -m "Resolve merge conflict in file.txt"

Method 2: Accept One Side

# Accept current branch (HEAD)
git checkout --ours filename.txt

Accept incoming branch

git checkout --theirs filename.txt

git add filename.txt

Method 3: Using Merge Tool

git mergetool

Opens configured merge tool (vimdiff, kdiff3, etc.).

Configure tool:

git config --global merge.tool vimdiff

Method 4: Using VS Code

VS Code shows conflict resolution UI:

1. Click "Accept Current Change" 2. Click "Accept Incoming Change" 3. Click "Accept Both Changes" 4. Click "Compare Changes"

Visualizing Conflicts

See Difference Between Versions

# See what changed in HEAD
git diff --ours

See what changed in incoming

git diff --theirs

See both together

git diff --base

After Resolution

Stage Resolved Files

git add filename.txt

Complete Merge

git commit

Git provides a default merge commit message:

Merge branch 'feature-branch' into main

Please enter the commit message for your changes. Lines starting

with '#' will be ignored, and an empty message aborts the commit.

Save and close to complete.

Abort if Needed

git merge --abort

Preventing Conflicts

Keep Branches Small

Merge frequently, not at the end.

Communicate with Team

Coordinate who works on what files.

Use Feature Branches

Isolate work to reduce conflicts.

Pull Before Push

git pull --rebase origin main

Keep your branch updated.

Common Scenarios

Scenario 1: Same Line Changed

<<<<<<< HEAD
const greeting = "Hello";
=======
const greeting = "Hi";
>>>>>>> feature

Resolution: Choose one or combine:

const greeting = "Hello";

Scenario 2: File Deleted by One Branch

If deleted in one branch, modified in another:

  • git add file.txt - Keep the file
  • git rm file.txt - Remove the file

Scenario 3: Multiple Files

Resolve each file:

git add file1.txt
git add file2.txt
git commit -m "Resolved conflicts"

Practice Example

# Create conflict scenario
echo "Line 1" > test.txt
git add . && git commit -m "Initial"

git checkout -b feature echo "Feature line" >> test.txt git add . && git commit -m "Feature"

git checkout main echo "Main line" >> test.txt git add . && git commit -m "Main change"

Try merge (will conflict)

git merge feature

Resolve

nano test.txt # Edit to keep both or choose one git add test.txt git commit -m "Resolve conflict"

Summary

When conflicts occur:

1. Don't panic - it's normal 2. Check git status to see conflicting files 3. Edit files to remove markers 4. Use git checkout --ours or --theirs for quick resolution 5. Stage and commit resolved files 6. Use git mergetool or VS Code for visual resolution

Next Lesson

Learn about rebase - an alternative to merging.

Quiz - Quiz - Resolving Merge Conflicts

1. What causes merge conflicts in Git?

2. What do the markers <<<<<<< and >>>>>>> indicate?

3. Which command accepts 'theirs' version during conflict resolution?

4. What is the recommended way to resolve conflicts visually?

5. After resolving conflicts, what is the next step?

Merging Branches