Merge conflicts

Resolve a merge conflict

Lesson 11 of 20 · 11 min · Module 3: Branches

Sometimes two branches change the same lines. Git will not guess which version you want: it stops and asks you. That is a conflict, and resolving one is a normal part of working with others.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Explain why conflicts happen
  • Read conflict markers
  • Resolve, stage and commit a merge

The idea

During a conflict, the file contains both versions between markers: <<<<<<< HEAD (your branch), =======, and >>>>>>> new-tagline (the other branch). Edit the file into the version you want and remove the markers, then git add it and run git commit to finish the merge. git status lists the conflicted files, and git merge --abort cancels the whole merge.

git merge new-tagline
cat site.yml
git add site.yml
git commit

01Why conflicts happen

Git merges changes to different lines on its own. When both branches change the same lines, it cannot know which version you want, so it stops and asks you. Nothing is lost: both versions are in the file.

git merge new-tagline
cat site.yml
Output
Auto-merging site.yml
CONFLICT (content): Merge conflict in site.yml
Automatic merge failed; fix conflicts and then commit the result.
title: My notes
<<<<<<< HEAD
tagline: Notes on code and coffee
=======
tagline: Notes on code and music
>>>>>>> new-tagline

02Read the markers

Between <<<<<<< HEAD and ======= is the version from your branch; between ======= and >>>>>>> new-tagline is the other branch's. Replace the whole block, markers included, with the text you want to keep.

03Mark it resolved and finish

git add tells Git the file is resolved, and git commit creates the merge commit. Changed your mind before committing? git merge --abort puts everything back as it was.

printf 'title: My notes\ntagline: Notes on code, coffee and music\n' > site.yml
git add site.yml
git status
Output
On branch main
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
	modified:   site.yml

Your turn

Merge new-tagline into main. Both branches changed the same line of site.yml, so the merge stops with a conflict. Resolve it so the file is exactly:
title: My notes
tagline: Notes on code, coffee and music
Then stage it and finish the merge with git commit.

What the checks look for

  • Starts the merge and gets the conflict
  • site.yml has both ideas and no conflict markers
  • The merge is finished with a merge commit
  • Nothing is left to commit
Try it in the editor →

Take this with you

A conflict is Git asking you to choose: edit the file, git add it, git commit.

Go a little further

During a conflict, run git diff: it shows the conflict with one column for each side. Then try git merge --abort and merge again.

Learn Git & GitHub for free

20 short lessons with real code and instant checks.

See the whole course →