Resolve Git Merge Conflicts Step by Step

Git and GitHub Essentials: Version Control for Beginners · Git Basics · 4 min read · Updated

A merge conflict

  1. Two branches changed the same line in different ways: one made the price 22, the other 25.
  2. When you merge, Git cannot decide which one is right.
  3. Git stops and marks the file with conflict markers that show both versions.
  4. You choose the final text, remove the markers, run git add, and commit to complete the merge.

Almost every beginner is afraid of the first merge conflict. It is not a disaster. It simply means: two people (or two branches) changed the same lines in different ways, and Git cannot know which version you want. It stops and asks you to decide. Once you have done it two or three times, it becomes a routine job. This lesson walks through one calmly.

The situation

A price file says Tea: 20. On the branch update-tea, someone changed it to 25. Meanwhile on main, someone changed it to 22. Both changes are on the same line, so Git cannot combine them automatically. Here is the real session.

$ git merge update-tea
Auto-merging price.txt
CONFLICT (content): Merge conflict in price.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status -s
UU price.txt
$ cat price.txt
<<<<<<< HEAD
Tea: 22
=======
Tea: 25
>>>>>>> update-tea
Coffee: 30
$ git add price.txt
$ git commit --no-edit
[main 1012b0b] Merge branch 'update-tea'
$ git log --oneline --graph
*   1012b0b Merge branch 'update-tea'
|\  
| * 586290f Tea costs 25
* | 0c40438 Tea costs 22
|/  
* c156a1b Add prices

Reading what Git tells you

  • CONFLICT (content): Merge conflict in price.txt names the file with the problem. Other files merged fine.
  • git status -s shows UU for the file: unmerged on both sides.
  • Git changed the file itself and added markers: the lines between <<<<<<< HEAD and ======= are your current branch's version (main: 22). The lines between ======= and >>>>>>> update-tea are the other branch's version (25).
  • The line Coffee: 30 was not marked, because both sides agree about it.

How to resolve it

  1. Open the file in your editor and find the markers.
  2. Decide what the final text should be. You can keep one side, the other, or write something new. We agreed on a price of 24.
  3. Delete all three marker lines (<<<<<<<, =======, >>>>>>>) and leave only the final text. The file must look like normal text again.
  4. Save, then git add price.txt to tell Git the file is resolved.
  5. Finish with git commit --no-edit, which uses the default merge message. In the log graph, you can see the two lines joining at the merge commit.

Changed your mind? Abort

If you start a merge and feel lost, run git merge --abort. Git puts everything back as it was before the merge. There is no shame in a fresh start, and it is much better than guessing.

How to have fewer conflicts

  • Pull the latest changes often, so your branch does not drift far from main.
  • Keep branches short-lived. A branch that lives for weeks collects conflicts.
  • Make small commits and small pull requests.
  • Talk to your team: two people changing the same file at the same moment is a signal to coordinate.
  • Do not reformat whole files, for example by changing indentation everywhere, because the diff touches every line and creates conflicts for others.

Conflicts are about people, not only code

The tool shows the clash, but the answer is often a decision. Which price is right? Which wording should stay? If you are not sure, ask the person who made the other change. Do not simply pick one side to make the conflict go away, because you could lose someone's work.

Your task

  1. Create a project with price.txt containing Tea: 20. Make two branches that change that line differently and commit on each.
  2. Merge one into the other and see the conflict. Run git status and open the file.
  3. Resolve it by writing a final value, remove the markers, add and commit.
  4. Repeat, but this time run git merge --abort and check that everything returned to its state before the merge.

How to check it worked

  • The final file has no <<<<<<<, ======= or >>>>>>> lines.
  • git status says the working tree is clean after the merge commit.
  • After git merge --abort, the file contents are what they were before the merge.

Frequently asked questions

Why do merge conflicts happen?

Two branches changed the same lines in different ways, or one deleted what the other changed. Git cannot decide which to keep, so it asks you.

How do I cancel a merge in the middle of a conflict?

Run git merge --abort. Git returns your files to the state they had before you started the merge.