Lesson 20 +20 XP

Merge Conflicts

Merge Conflicts

A merge conflict happens when two branches changed the same lines of the same file in different ways, and Git cannot decide which one to keep.

When do they happen?

Conflicts are normal. They usually happen when two people (or two branches) edit the same part of a file at the same time.

What Git tells you

git status

Git lists the conflicted files as "both modified". Inside the file, Git marks the conflict:

<<<<<<< HEAD
my version of the line
=======
their version of the line
>>>>>>> feature-login
  • <<<<<<< HEAD is the start of YOUR version.
  • ======= separates the two versions.
  • >>>>>>> feature-login is the end of THEIR version.

Fix the conflict

  1. Open the file in your editor.
  2. Decide which lines to keep (or write something new).
  3. Delete the <<<<<<<, =======, and >>>>>>> markers.
  4. Save the file.

Tell Git it is fixed

git add file.txt
git commit

Staging the file marks the conflict as resolved, and the commit finishes the merge.

TL;DR

  • Conflicts occur when the same lines are changed differently.
  • Git marks them with <<<<<<<, =======, and >>>>>>>.
  • Fix the file by hand, then stage and commit.
  • Conflicts are normal and not scary.