Resolving Merge Conflicts Step-by-Step with Conflict Markers
Resolving Merge Conflicts Step-by-Step with Conflict Markers
Merge conflicts are not software bugs or Git errors—they are a natural, protective feature of version control. When two developers change the exact same lines of the same file in different ways, Git refuses to guess which version is correct and safely halts, asking the developer to resolve the conflict.
1. What Causes a Merge Conflict?
A conflict occurs when:
- 1Developer A and Developer B branch from the same base commit.
- 2Developer A edits line 15 in
server.jsand commits it tomain. - 3Developer B edits line 15 in
server.jsin a conflicting way onfeature-branch. - 4When merging
feature-branchintomain, Git encounters conflicting modifications on line 15 and pauses the merge:
2. Anatomy of Git Conflict Markers
Git opens the conflicting file and marks the disputed lines with special ASCII delimiter markers:
Breakdown of the Markers:
<<<<<<< HEAD: Marks the beginning of the conflicting section. Shows the version on the branch you are currently standing on (main).=======: The divider separating the two competing versions.>>>>>>> feature-backend: Marks the end of the conflict section and identifies the incoming branch that introduced the alternative.
3. The 4-Step Method to Resolve Conflicts
Step 1: Identify Conflicted Files
Run git status to see which files have unresolved conflicts (listed under "Unmerged paths"):
Step 2: Open and Edit the Conflicting File
Open src/server.js in your text editor (such as Visual Studio Code). Decide how the code should look:
- Keep the
HEADversion? - Keep the incoming version?
- Combine ideas from both versions?
Critical Rule: You must completely delete all <<<<<<<, =======, and >>>>>>> lines from the file!
Cleaned resolution:
Step 3: Stage the Resolved File
Staging the file informs Git that the conflict has been resolved:
Step 4: Finalize the Merge Commit
Git will pre-populate a standard merge commit message: Merge branch 'feature-backend' into main. Save and exit the editor. Your merge is now complete!
4. Aborting a Merge (--abort)
If a merge is overwhelming or you started it by mistake, you can reset your working tree back to the state before the merge was attempted:
This safely rolls back the merge without losing any previous work.
Practice Quiz
<<<<<<< HEAD and ======= represent?<<<<<<<, =======, >>>>>>>) when resolving a conflict?What is GitHub? Remote Hosting, Social Coding & Open Source
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.