01 / 20

Resolving
Merge Conflicts

Learn how Git detects conflicting changes and how to read conflict markers, choose the right code, and complete a clean merge.

Computer Science Git Fundamentals

What Is a Merge Conflict?

A merge conflict happens when two branches change the same lines of the same file. Git cannot automatically decide which version to keep, so it pauses and asks you to resolve the disagreement.

When Do Conflicts Happen?

A B main C E feature D F ! Conflict Same lines changed in both branches Commit D (main) line 5: let x = 10; Commit E (feature) line 5: let x = 20;

Why Resolving Conflicts Matters

Team Collaboration
Multiple developers push changes daily. Conflicts are inevitable on any real project.
Code Integrity
A bad resolution can silently break features or introduce subtle bugs.
Workflow Flow
Until the conflict is resolved, the merge is blocked and nothing moves forward.

The Three Conflict Markers

<<<<<<< HEAD // Your current branch's version let greeting = "Hello"; ======= // The incoming branch's version let greeting = "Hi there"; >>>>>>> feature-branch
<<<<<<< HEAD
Marks the start of your current branch's version (the "ours" side).
=======
The divider between the two competing versions.
>>>>>>>
Marks the end of the incoming branch's version (the "theirs" side).

Anatomy of a Conflict Block

app.js 1 2 3 4 5 6 7 8 9 10 11 function greet() { <<<<<<< HEAD let msg = "Hello"; return msg; ======= let msg = "Hi there"; return msg + "!"; >>>>>>> feature console.log(greet()); } module.exports = greet; Start marker HEAD (ours) Separator Incoming (theirs) End marker

Ours vs. Theirs: Key Distinction

Ours (HEAD) Theirs (Incoming)
Position Above ======= Below =======
Source Your current branch The branch being merged in
Marker <<<<<<< HEAD >>>>>>> branch
Analogy "What I wrote" "What they wrote"

Three Resolution Strategies

Accept Ours
Delete the incoming code and all markers. Keep only the code above the separator.
Accept Theirs
Delete your code and all markers. Keep only the code below the separator.
Combine Both
Manually merge the logic from both sides, then remove all conflict markers.

How to Resolve Step by Step

1
Open the conflicted file. Git tells you which files have conflicts after a failed merge.
2
Find the conflict markers. Search for <<<<<<< to locate each conflict block.
3
Decide which code to keep. Read both versions and choose ours, theirs, or a combination.
4
Delete all marker lines. Remove <<<<<<<, =======, and >>>>>>> entirely.
5
Stage and commit. Run git add on the file, then git commit to finalize the merge.

Example 1: Accept Ours

Your team decided the main branch has the correct color. Remove the incoming code and all markers:

// BEFORE (conflicted) <<<<<<< HEAD const color = "blue"; ======= const color = "red"; >>>>>>> feature
// AFTER (resolved) const color = "blue";

Example 2: Accept Theirs

After reviewing, the feature branch has a better implementation. Keep the incoming code:

// BEFORE (conflicted) <<<<<<< HEAD function add(a, b) { return a + b; } ======= const add = (a, b) => a + b; >>>>>>> feature
// AFTER (resolved) const add = (a, b) => a + b;

Example 3: Combine Both

Sometimes both sides add different code you want to keep. Merge the logic manually:

// BEFORE (conflicted) <<<<<<< HEAD const API_URL = "/api/v2"; ======= const TIMEOUT = 5000; >>>>>>> feature
// AFTER (resolved: keep both!) const API_URL = "/api/v2"; const TIMEOUT = 5000;

The Full Git Workflow

git merge feature-branch CONFLICT Merge halted Edit file(s) Remove markers git add . Stage resolved files git commit Merge complete ✓ $ git merge feature → CONFLICT in app.js $ nano app.js → $ git add app.js → git commit

Common Mistakes to Avoid

✕ Leaving Markers
Forgetting to delete <<<<<<<, =======, or >>>>>>> lines. The code won't run with these in it.
✕ Deleting Wrong Code
Accidentally removing the version you actually wanted to keep. Always read both sides before editing.
✕ Skipping git add
Editing the file but forgetting to stage it. Git still thinks the conflict is unresolved.

Summary: Resolution at a Glance

Step Action Result
1. Detect Run git merge Git reports conflicted files
2. Locate Search for <<<<<<< Find each conflict block
3. Decide Read ours vs. theirs Choose strategy
4. Edit Keep desired code Delete all markers
5. Finalize git add + git commit Merge complete ✓

Question 1 of 5

Which marker line separates the two conflicting versions inside a conflict block?

Question 2 of 5

If you want to accept the feature branch's value, what should the resolved file contain?

Question 3 of 5

After manually editing a conflicted file, what two Git commands do you need to run to complete the merge?

Question 4 of 5

What happens if you leave conflict markers in a JavaScript file and try to run it?

Question 5 of 5

What is the best resolution in this case?