Lesson

Git Merge Conflicts

10 min reading
Free Course

Git Merge Conflicts: Resolving Competing Edits Confidently

A merge conflict happens when two branches modify the exact same lines of a file—or when one branch deletes a file that another branch edited—so Git cannot automatically decide which version to keep. Rather than guessing and silently overwriting code, Git pauses the merge and asks you to choose the final result.

Understanding Conflict Markers

Suppose main and feature/header-nav both edited the navigation block inside index.html. When you run git merge feature/header-nav, Git writes markers directly into index.html:

<<<<<<< HEAD
<p>Welcome to Starter Site v1.1</p>
=======
<p>Explore Starter Site Documentation</p>
<object data="assets/banner.svg" type="image/svg+xml"></object>
>>>>>>> feature/header-nav
  • Everything between the top angle-bracket marker (HEAD) and the ======= divider comes from your current active branch.
  • Everything between the ======= divider and the bottom angle-bracket marker (feature/header-nav) comes from the incoming branch you are merging.

Step-by-Step Conflict Resolution

  1. Run git status to see every file listed under Unmerged paths.
  2. Open each conflicted file in VS Code or your editor (VS Code highlights buttons for Accept Current, Accept Incoming, or Accept Both).
  3. Edit the file so only the exact desired code remains, and delete all three conflict marker lines (the opening HEAD line, the equals divider line, and the closing branch line).
  4. Save the file, run tests or inspect the page in your browser, then stage and commit the resolved merge:
git add index.html
git status
git commit

If you want to back out and try again later, run git merge --abort.

Preventing Frequent Merge Conflicts

Keep feature branches short-lived, pull updates from main regularly, and agree on consistent formatting (such as Prettier or EditorConfig) so automatic whitespace reformatting does not collide across branches.

Frequently Asked Questions

What happens if I forget to delete a conflict marker line before committing?

Git still lets you commit the file once you run git add, which breaks your HTML, CSS, or code syntax immediately. Always search your file for leftover markers or run a linter before committing a resolved merge.

Can I pick one branch's entire file automatically during a conflict?

Yes. Run git checkout --ours styles.css to keep your current branch's file, or git checkout --theirs styles.css to take the incoming branch's version, then run git add styles.css.

Why did I get a conflict during git rebase or git pull?

Rebasing and pulling both apply commits from another timeline onto yours, so the exact same conflict markers appear. Resolve the file, run git add <file>, and finish with git rebase --continue (or git commit for a merge pull).

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum