CPCODELAB
Git & GitHub

Understanding and Resolving Merge Conflicts

12 min read

When Conflicts Happen

A merge conflict occurs when the same lines of the same file were changed differently on both branches. Git cannot decide which version is correct, so it pauses the merge and marks the conflicting sections.

Reading Conflict Markers

text
<<<<<<< HEAD
const greeting = "Hello, world";
=======
const greeting = "Hi there!";
>>>>>>> feature/login
  1. <<<<<<< HEAD — start of YOUR current branch's version.
  2. ======= — divider between the two conflicting versions.
  3. >>>>>>> feature/login — end of the INCOMING branch's version.

Resolving a Conflict Step by Step

  1. Run git status to see which files are conflicted (marked UU).
  2. Open each conflicted file and decide what the final content should be — keep one version, the other, or combine them.
  3. Delete all conflict markers (<<<<<<<, =======, >>>>>>>).
  4. Stage the resolved file with git add <file>.
  5. Once all conflicts are resolved, git commit to finalise the merge.
bash
git status                  # see conflicted files
# edit files to resolve
git add src/utils.ts
git commit -m 'merge feature/login into main'

VS Code's built-in merge editor highlights conflicts visually and provides one-click "Accept Current", "Accept Incoming", or "Accept Both" buttons. Use it — it is much faster than editing raw markers.

If you get stuck in a conflict and want to start over, run git merge --abort to restore your branch to the state before the merge began.

Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises