SECTION 1
Historical Context & Motivation
When people work together on a software project, they often edit the same files. Before modern tools existed, developers had to manually coordinate who was editing what—sometimes by literally shouting across the room, "Hey, I'm editing that file right now!" This approach didn't scale. As projects grew from a handful of files to thousands, the software industry needed a better way to handle simultaneous edits. The concept of version control emerged to solve this problem, and with it came the challenge of merge conflicts—situations where two people change the same part of a file and the computer can't automatically figure out which change to keep.
1972 • SCCS — First Version Control
1986 • CVS Introduces Merging
2005 • Git Is Born
2008 • GitHub Launches
Today, nearly every JavaScript project is managed with Git. Whether you're building a React app, a Node.js server, or a simple webpage, you'll eventually encounter a merge conflict. The question isn't if you'll face a conflict—it's when. So how do you read, understand, and fix a merge conflict in a text file? That's exactly what this lesson covers.
SECTION 2
Core Principles & Definitions
Before you can fix a merge conflict, you need to understand a few foundational ideas about how Git tracks changes and why conflicts happen in the first place. A branch in Git is like a separate copy of your project where you can make changes without affecting the main codebase. When you're done, you merge your branch back into the main branch. Git is usually smart enough to combine changes automatically, but when two branches modify the same lines of the same file, Git can't decide which version to keep—that's a merge conflict.
Branch
Merge
Conflict Markers
<<<<<<<, =======, and >>>>>>>.Resolution
KEY TAKEAWAY
SECTION 3
Visual Explanation — How a Conflict Forms
"blue" and "red".In the diagram above, notice how both branches stem from the same starting point (commit B). Your branch changes line 5 to color = "blue", while their branch changes the exact same line to color = "red". Git handles hundreds of non-overlapping changes automatically, but when two changes touch the same line, it stops and asks you for help. The bottom box shows what your file actually looks like after a failed merge—filled with those special conflict markers.
SECTION 4
How Conflict Markers Work
When Git encounters a merge conflict, it modifies the conflicted file by inserting three special markers. Understanding these markers is the key to resolving any conflict. Let's break down exactly what each marker means and where it appears.
The Three Conflict Markers
The first marker is <<<<<<< HEAD. Everything between this line and the next marker is your version of the code—the changes on your current branch. The word HEAD refers to the latest commit on the branch you're currently on.
The second marker is =======. This is the divider between your changes and their changes. Think of it like a fence separating two yards.
The third marker is >>>>>>> branch-name. Everything between the ======= and this line is their version of the code—the changes from the branch you're trying to merge in. The branch name tells you where those changes came from.
Full Conflict Pattern
<<<<<<< HEAD
your changes here
=======
their changes here
>>>>>>> feature-branch
You must remove all three marker lines and decide which code to keep. You can keep yours, theirs, both, or write something entirely new.The Resolution Process
- Open the file — Look for the
<<<<<<<markers. Your text editor or IDE will usually highlight these. - Read both versions — Understand what your branch changed and what their branch changed. Ask yourself: what was the intent behind each edit?
- Choose and edit — Keep your version, keep their version, combine both, or rewrite the section entirely.
- Remove ALL markers — Delete every
<<<<<<<,=======, and>>>>>>>line. Leaving them in will cause syntax errors. - Stage and commit — Run
git add <file>thengit committo finalize the merge.
SECTION 5
Anatomy of a Conflicted File
Let's look at a realistic example. Imagine you and a teammate are both working on a JavaScript file called config.js. You're on a branch called update-theme and your teammate is on a branch called dark-mode. Both of you edited the same section of the file.
Notice that the lines outside the conflict markers (lines 1–3, 11–12) are completely normal. Git doesn't touch code that wasn't changed by both branches. Only the overlapping changes—lines 5–6 in your branch and lines 8–9 from the dark-mode branch—are wrapped in markers. A single file can have multiple conflict blocks if there are several overlapping sections. You need to resolve each one individually.
SECTION 6
Worked Example — Resolving a Conflict Step by Step
Let's walk through resolving the conflict from the previous section. We'll assume you've decided that the correct resolution is to use their theme name ("midnight") but keep your font size (16). This is a very common scenario—combining the best parts of both changes.
Resolving the config.js Merge Conflict
- 1
Step 1 — Identify the Conflict
After runninggit merge dark-mode, Git outputs a message likeCONFLICT (content): Merge conflict in config.js. Rungit statusto confirm which files have conflicts. Openconfig.jsin your text editor.The file shows conflict markers around lines 4–10. - 2
Step 2 — Read Both Versions
Between<<<<<<< HEADand=======, your branch setstheme: "ocean"andfontSize: 16. Between=======and>>>>>>> dark-mode, their branch setstheme: "midnight"andfontSize: 14.You understand both sides. Their theme name is better; your font size is better. - 3
Step 3 — Edit the File
Delete all three marker lines (<<<<<<<,=======,>>>>>>>) and write the combined result. Your file should now read:theme: "midnight",fontSize: 16,The conflict zone is replaced with a single, clean version that combines both contributions. - 4
Step 4 — Verify the File
Scan the entire file for any remaining<<<<<<<markers. Make sure the JavaScript syntax is valid—no missing commas, no broken brackets. You can run the file withnode config.jsto check for syntax errors.No markers remain. The file is clean JavaScript. - 5
Step 5 — Stage and Commit
Rungit add config.jsto tell Git you've resolved the conflict, then rungit commit. Git will auto-populate a merge commit message. You can accept it or customize it to explain your resolution choices.The merge is complete. Both branches are unified.
Pro Tip
SECTION 7
Resolution Strategies Compared
When you encounter a merge conflict, you have several options for how to resolve it. Each strategy is appropriate in different situations. Understanding when to use each approach will save you time and prevent bugs.
| Strategy | What You Do | When to Use It |
|---|---|---|
| Accept Yours | Delete their changes and the markers. Keep only the code between <<<<<<< HEAD and =======. | When you're confident your changes are correct and their changes are outdated or wrong. |
| Accept Theirs | Delete your changes and the markers. Keep only the code between ======= and >>>>>>>. | When their version is better, or you want to defer to a teammate's expertise on that part of the code. |
| Combine Both | Manually merge the best parts of both versions into a new version. Remove all markers. | When both branches made valuable but different changes that should coexist. This is the most common approach in real projects. |
| Rewrite | Discard both versions and write entirely new code that addresses the intent of both changes. | When the conflict reveals a deeper design issue and neither version is quite right. |
| Abort the Merge | Run git merge --abort to undo the entire merge attempt and return to the state before you merged. | When the conflict is too complex to resolve right now, or you need to talk to your teammate first. |
Five common merge conflict resolution strategies
KEY TAKEAWAY
SECTION 8
Connection to Advanced Conflict Resolution
The simple text-based conflicts we've covered in this lesson are just the beginning. As your projects grow, you'll encounter more complex situations that require additional tools and strategies. Here's a preview of what lies ahead.
| What You Learned Today | What Comes Next |
|---|---|
| Conflicts in a single text file with clear markers | Conflicts across multiple files in a single merge, where changes in one file depend on changes in another |
| Editing conflict markers manually in a text editor | Using visual merge tools (VS Code's built-in tool, Beyond Compare, Meld) to compare versions side by side |
| Two-way conflicts (yours vs. theirs) | Three-way merges that also show the common ancestor—the original version both branches started from |
Resolving conflicts during git merge | Resolving conflicts during git rebase, git cherry-pick, and pull request workflows |
| Text-based conflicts in .js files | Binary file conflicts (images, compiled assets) and package-lock.json conflicts |
Intro vs. advanced merge conflict skills
Don't worry about mastering all of these right away. The foundation you're building now—understanding markers, reading both sides, choosing a resolution, and committing—applies to every type of merge conflict you'll ever encounter. More advanced scenarios just add layers of context, but the core process remains the same.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
Show answer
Question 2: BASIC CALCULATION
greeting.js, write out exactly what the file should look like after resolving the conflict by accepting the incoming (their) change:
<<<<<<< HEAD
const msg = "Hello, World!";
=======
const msg = "Welcome aboard!";
>>>>>>> feature-welcomeShow answer
const msg = "Welcome aboard!";
All three conflict marker lines (<<<<<<< HEAD, =======, and >>>>>>> feature-welcome) must be completely removed. The HEAD version ("Hello, World!") is also removed because we chose to accept the incoming change.Question 3: INTERMEDIATE
styles.js:
const styles = {
<<<<<<< HEAD
color: "navy",
background: "white",
=======
color: "teal",
background: "black",
>>>>>>> dark-theme
padding: 10,
};
You decide you want the color from the dark-theme branch ("teal") but the background from your branch ("white"). Write the resolved file.Show answer
const styles = {
color: "teal",
background: "white",
padding: 10,
};
This is a "combine both" resolution. You took the color value from the incoming branch and the background value from your branch. All conflict markers are removed, and the padding: 10 line was never part of the conflict so it stays unchanged. The file is now valid JavaScript.Question 4: APPLIED
git merge, you see conflicts in two files: index.js and utils.js. However, you're not sure what your classmate intended with their changes to utils.js. Describe the steps you would take and the Git commands you would use.Show answer
index.js since you understand both sides: open the file, choose the correct resolution, remove the markers, and run git add index.js. For utils.js, you should contact your classmate to discuss their intent before making changes. If you need to pause the entire merge, run git merge --abort to undo everything and return to your pre-merge state. Once you've discussed with your classmate, re-run the merge and resolve both files. Alternatively, you could resolve index.js, leave utils.js unresolved, communicate with your teammate, then resolve it—you don't have to abort the whole merge just because one file is tricky.Question 5: CRITICAL THINKING
Show answer
SUMMARY
Lesson Summary
Summary
A merge conflict occurs when two branches modify the same lines of the same file and Git cannot automatically determine which version to keep. Git marks the conflicted area with three conflict markers: <<<<<<< HEAD (start of your changes), ======= (divider), and >>>>>>> branch-name (end of their changes). To resolve a conflict, you read both versions, decide which code to keep—using strategies like accept yours, accept theirs, combine both, or rewrite—then delete all marker lines and finalize with git add and git commit.
Merge conflicts are a normal part of collaboration, not a sign of failure. The key skills are understanding the conflict marker format, reading both sides carefully, and making an intentional decision about the final code. With practice, resolving conflicts becomes a quick and routine part of your Git workflow.