Resolving merge conflicts

Intermediate

Introduction

Conflicts are a normal part of working in a team, not an error. They happen when two people change the same lines in different ways, and Git, rightly, refuses to guess which version is correct. Resolving them confidently is a core skill: it mostly means reading carefully and deciding what the code should say.

How it works

When a merge conflicts, Git stops before committing, and git status lists the files as both modified. In each such file, every conflicting region looks like this:

<<<<<<< HEAD
color = green
=======
color = red
>>>>>>> feature

The part between <<<<<<< and ======= is ours (the current branch, HEAD); the part between ======= and >>>>>>> is theirs (the branch being merged). Everything outside the markers merged cleanly.

To resolve, edit the file into the version you want: ours, theirs, both, or something new that combines them. Then remove all the markers, and tell Git:

git status                    # which files still have conflicts?
git add settings.txt          # mark this file as resolved
git commit                    # finish the merge (Git suggests a message)
git merge --abort             # or give up and go back to before the merge
git checkout --ours file.txt  # take our whole version of a file
git checkout --theirs file.txt

How does Git decide which lines conflict? It runs the same three-way rule as for files, but on lines, using the merge base: a line changed on only one side takes that side's version; a line changed identically on both sides is fine; a line changed differently on both sides conflicts. Neighbouring conflicting lines are grouped into one conflict block. Real Git first aligns the three versions with diffs so that inserted and deleted lines are handled too; the example and exercises use files whose lines correspond one to one, which keeps the rule easy to see.

Example

base = ["title = Notes", "color = blue", "size = 10", "font = serif"]
ours = ["title = Notes", "color = green", "size = 12", "font = serif"]
theirs = ["title = My notes", "color = red", "size = 12", "font = serif"]

merged, block = [], None
conflicts = 0
for b, o, t in zip(base, ours, theirs):
    if o == t or t == b:
        line, clash = o, False
    elif o == b:
        line, clash = t, False
    else:
        clash = True
    if clash:
        if block is None:
            block = ([], [])
        block[0].append(o)
        block[1].append(t)
        continue
    if block is not None:
        merged += ["<<<<<<< ours", *block[0], "=======", *block[1], ">>>>>>> theirs"]
        conflicts += 1
        block = None
    merged.append(line)
if block is not None:
    merged += ["<<<<<<< ours", *block[0], "=======", *block[1], ">>>>>>> theirs"]
    conflicts += 1
for line in merged:
    print(line)
print(f"-- {conflicts} conflict(s); edit the file, remove the markers, then git add and git commit")
Open in compiler

Line by line

  1. The base, ours and theirs versions have four corresponding lines each.
  2. The title changed only on their side, so their version is taken without a conflict.
  3. The colour changed on both sides differently, so it conflicts.
  4. The size changed on both sides in the same way, so it is not a conflict.
  5. The conflicting line is wrapped in markers, and the program counts one conflict.

Expected result

title = My notes
<<<<<<< ours
color = green
=======
color = red
>>>>>>> theirs
size = 12
font = serif
-- 1 conflict(s); edit the file, remove the markers, then git add and git commit

In the real world

Editors such as VS Code highlight conflict blocks with buttons to accept ours, theirs or both, and GitHub can resolve simple conflicts in the browser. Teams reduce conflicts by keeping branches short-lived, merging main in often, and avoiding sweeping reformatting changes mixed with real work. Automated formatters run by everyone also prevent many conflicts.

Common mistakes

  • Committing a file that still contains markers; search for <<<<<<< before committing.
  • Always taking one side without reading the other, silently throwing away a colleague's fix.
  • Resolving the text but not testing: the merged code may compile yet be wrong.
  • Mixing up ours and theirs during a rebase, where their meanings are swapped.

Best practices

  • Understand both changes before deciding; talk to the other author if unsure.
  • Run the tests after resolving, before committing.
  • Keep branches small and up to date with main to make conflicts rare and small.

Summary

Git marks conflicts with <<<<<<<, ======= and >>>>>>>, ours first and theirs second. You resolve by editing to the right result, removing the markers, adding the file and committing, or abort with git merge --abort. A line conflicts when both sides changed it differently from the merge base.

Quick check

1. Which version is between <<<<<<< HEAD and =======?
2. After editing a conflicted file, what tells Git it is resolved?
3. Both branches changed a line in exactly the same way. Is it a conflict?
4. How do you give up a conflicted merge?

Key takeaways

  • Between <<<<<<< and ======= is our version; between ======= and >>>>>>> is theirs.
  • Resolve by editing to the right result, removing every marker, then git add and git commit.
  • A line conflicts only when both sides changed it differently compared with the merge base.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.