Class 17 of 50 • Branching & Merging

Git Merge Conflicts: Understanding, Detecting & Resolving Step-by-Step

Merge conflicts are not bugs; they are safety mechanisms. When two developers modify the exact same lines of code in the same file in different ways, Git refuses to guess which version is correct. Instead, Git pauses the merge and asks the human developer to resolve the conflict.

How Git Flags a Merge Conflict

When a conflict occurs during git merge, Git alerts you in the terminal:

$ git merge feature/header
Auto-merging src/Navbar.js
CONFLICT (content): Merge conflict in src/Navbar.js
Automatic merge failed; fix conflicts and then commit the result.

Running git status shows unmerged paths:

$ git status
Unmerged paths:
  (use "git add ..." to mark resolution)
	both modified:   src/Navbar.js

Anatomy of Conflict Markers

If you open src/Navbar.js in your text editor, you will see Git's standard conflict markers:

<<<<<<< HEAD
// Your current branch version (main)
const BRAND_COLOR = "#7d1935";
=======
// The incoming branch version (feature/header)
const BRAND_COLOR = "#0284c7";
>>>>>>> feature/header

Deciphering the Markers:

  • <<<<<<< HEAD: Marks the beginning of the conflicting code from your currently checked out branch (e.g. main).
  • =======: The divider separating the two competing versions.
  • >>>>>>> feature/header: Marks the end of the incoming conflicting code from the branch being merged in.

Step-by-Step Conflict Resolution Workflow

  1. Open the conflicting file in your code editor (like VS Code).
  2. Discuss with your teammate (or decide): Which color is correct? Or should we combine them?
  3. Edit the code manually: Delete the conflict marker lines (<<<<<<<, =======, >>>>>>>) and keep only the final, working code.
  4. Save the file.
  5. Stage the resolved file: git add src/Navbar.js.
  6. Finalize the merge: Run git commit. Git opens your editor with a pre-populated commit message: "Merge branch 'feature/header'". Save and exit to complete!

Pro Tip: Enable diff3 for Superior Context

By default, Git shows only your version and their version. By configuring diff3 style, Git also shows the original common ancestor version before either person touched it:

git config --global merge.conflictstyle diff3

Now, conflict blocks display three sections:

<<<<<<< HEAD
const BRAND_COLOR = "#7d1935";
||||||| merged common ancestors
const BRAND_COLOR = "#ffffff";
=======
const BRAND_COLOR = "#0284c7";
>>>>>>> feature/header

Seeing that the original color was #ffffff makes it immediately obvious what each developer changed!

How to Bail Out Safely

If you get confused during a complex conflict resolution and want to reset back to a clean state as if the merge never happened, run:

git merge --abort

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Fast-Forward & 3-Way Merges

Learn how Git identifies the best common ancestor during automatic merges.

Inspecting Diffs (git diff)

View the exact differences between conflicting files and working trees.

Rebase Conflicts & --continue

Resolve conflicts one-by-one during interactive or branch rebasing.

Git Troubleshooting & Master FAQ

Abort failed merges safely with git merge --abort and reset clean state.

Frequently Asked Questions (FAQ) - Student & Interview Review

Conflicts occur when two branches alter the exact same lines in a file, or when one branch modifies a file while another branch deletes it.
Yes! Running `git mergetool` launches your configured visual three-way merge tool (such as VS Code, Meld, KDiff3, or Beyond Compare).
Yes! Visual Studio Code displays 4 clickable quick actions above any conflict block: 'Accept Current Change', 'Accept Incoming Change', 'Accept Both Changes', and 'Compare Changes'.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to git rebase Fundamentals → Previous: ← Merge Strategies | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!