Everyone Can Learn Program
中文 Start learning
Core · GitLesson 4 of 6Free to read

Merge conflicts · Git and GitHub

What do you do when two changes collide?

Course contents

Why you need it

When two people change the same lines, Git can't know which version is right, so it stops and asks you. Conflicts are normal on any team, not a sign that something went badly wrong, and resolving one calmly takes a minute once you know the steps.

The intuition

Git marks the clash inside the file:

<<<<<<< HEAD
your version (the branch you are on)
=======
their version (the branch you are merging)
>>>>>>> new-title

The steps are always:

  1. git status: which files conflict?
  2. Edit each file: keep one side, the other, or a mix, and delete the marker lines.
  3. git add FILE to mark it solved.
  4. git commit to finish the merge.

Changed your mind? git merge --abort puts everything back as before the merge.

Definition

Merge conflict

Both branches changed the same part of a file. Git stops the merge until you choose the result.

Conflict markers

The lines <<<<<<<, ======= and >>>>>>> that Git writes into a file to show both versions. They must be removed before you commit.

Examples

Meet a conflict

Run it in the lesson

main renamed the shop, new-title renamed it differently. Merge, and look.

git merge new-title
git status
cat README.md
Step by step
  1. L1CONFLICT: Git stops halfway.
  2. L2README.md is listed as both modified.
  3. L3Both versions, between the markers.<<<<<<< HEAD

Write the final version, mark it solved, commit.

git merge new-title
echo "# My Best Shop" > README.md
git add README.md
git commit -m "merge new-title"
git log --oneline
Step by step
  1. L2The final text, with no markers left.
  2. L3add means: this conflict is solved.
  3. L4The merge commit completes the merge.

Common mistakes

Committing the markers

If <<<<<<< is still in the file, you've committed a broken file. Search for it before git add.

Keeping only your side by reflex

The other change was made for a reason. Read both sides; often the right answer combines them.

Quick quiz

1. You fixed the file. What tells Git the conflict is resolved?

  1. Agit add FILE
  2. Bgit merge again
  3. Cgit restore FILE
Show the answer

Answer: A

Staging the fixed file marks it resolved; then commit.

2. What does the last command print?

git merge new-title
git merge --abort
cat README.md
  1. A
    # My Shop
  2. B
    # Best Shop
  3. C
    <<<<<<< HEAD
    # My Shop
    =======
    # Best Shop
    >>>>>>> new-title
Show the answer

Answer: A

--abort returns to exactly how things were before the merge: main's version.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictRead the markers

    After this merge, which line between the markers comes from the branch new-title?

  2. ModifyFinish the merge

    This resolves the conflict but forgets two steps, so the merge never finishes. Add them so the merge is committed with the message merge new-title.

  3. WriteKeep both ideas

    Merge new-title into main and resolve the conflict so README.md contains exactly the single line # My Best Shop. Commit the merge.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Merge conflict合并冲突Two branches changed the same lines; you choose the result.
Abort中止Cancel an operation and go back to before it started.