Resolving merge conflicts

Topic 4 of 4 in Branching and merging7 min readBeginnerFree course

Overview

Most merges are automatic. But when two branches change the same lines of the same file in different ways, Git can’t know which version is right — so it stops and asks you. That’s a merge conflict. Conflicts sound scary, but they’re a normal part of teamwork, and resolving one is a straightforward editing job once you can read the markers.

When conflicts happen

  • Both branches changed the same line or adjacent lines differently.
  • One branch edited a file and the other deleted it.
  • Both branches added a file with the same name but different content.

Changes to different files, or different parts of the same file, merge automatically.

Recognising a conflict

Terminal
git merge feature/tagline
Text
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Auto-merging prices.txt
CONFLICT (content): Merge conflict in prices.txt
Automatic merge failed; fix conflicts and then commit the result.

The merge is now paused. Files that merged cleanly are already staged; conflicted ones are waiting for you. git status lists them:

Text
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   index.html
	both modified:   prices.txt

Reading conflict markers

Git writes both versions into the file, with markers around them:

Anatomy of a merge conflict. In index.html, between <h1> and </h1>: the line <<<<<<< HEAD starts your branch’s version (where HEAD is), Welcome to Academy; the ======= line is the divider; then Learn to code with Academy is the branch being merged in, ending with >>>>>>> feature. To fix it: 1, open the file and pick the final text; 2, delete all three marker lines; 3, git add index.html; 4, git commit (or git merge –continue). Changed your mind? git merge –abort.

  • <<<<<<< HEAD to =======: your version — the branch you’re on (main).
  • ======= to >>>>>>> feature/tagline: the incoming version — the branch you’re merging.

Everything outside the markers merged without problems.

Resolving

  1. Open the file and decide what the final content should be. It might be your side, their side, a combination, or something new.
  2. Delete all three marker lines. A leftover <<<<<<< will break your code (and is embarrassing in a pull request).
  3. Mark it resolved by staging it: git add index.html.
  4. When every conflicted file is staged, finish the merge:
Terminal
git commit          # Git pre-fills the message: "Merge branch 'feature/tagline'"
# or
git merge --continue

Taking one side

When you know one whole version is right:

Terminal
git checkout --ours prices.txt      # keep the current branch's version
git checkout --theirs prices.txt    # take the incoming branch's version
git add prices.txt

(Careful: during a rebase, “ours” and “theirs” are swapped, because rebase replays your commits onto the other branch. Read the file before choosing.)

Seeing more context

The standard markers show your side and theirs, but not what the line looked like before either change. That’s often the key to a good resolution. Turn on the three-way style:

Terminal
git config --global merge.conflictStyle zdiff3

Now conflicts (in Git 2.35 and later) include a middle section showing the original:

Text
<<<<<<< HEAD
<h1>Welcome to Academy</h1>
||||||| 1a2b3c4
<h1>Academy</h1>
=======
<h1>Learn to code with Academy</h1>
>>>>>>> feature/tagline

Now you can see that both sides changed the same original heading, so the right answer probably combines them.

Giving up

If the merge is more than you expected, back out — nothing is lost:

Terminal
git merge --abort

Everything returns to how it was before git merge.

Tools

  • Your editor: VS Code and JetBrains IDEs highlight conflicts with buttons like “Accept Current”, “Accept Incoming”, “Accept Both”, and a three-way merge editor.
  • git mergetool opens a configured merge tool for each conflicted file.
  • GitHub can resolve simple conflicts in the browser from a pull request.

Whatever tool you use, the steps are the same: decide the final content, remove the markers, stage, commit.

After resolving: check it works

Git only checks that the markers are gone (and not even that, if you git add too quickly). The code might still be wrong — both sides may have renamed the same function differently, for example. Run the tests or at least run the app before committing the merge.

Newer Git versions can help with the staging step: git add --resolved (Git 2.56+) stages only the files that had conflicts, so unrelated edits in your working directory stay unstaged.

Avoiding painful conflicts

  • Merge often. Short-lived branches and frequent merges from main keep conflicts small and fresh in your mind.
  • Keep commits focused. A commit that reformats a whole file conflicts with everyone.
  • Agree on formatting. An automatic formatter stops “conflicts” that are only whitespace or quote style.
  • Talk to each other. If two people need to change the same file heavily, coordinate.
  • Let Git remember: git config --global rerere.enabled true (“reuse recorded resolution”) makes Git remember how you resolved a conflict and reapply it if the same conflict appears again — handy when rebasing or re-merging.

Mindset: a conflict isn’t an error. It’s Git saying “two people changed this — you decide.” Read both sides, combine thoughtfully, test, commit.

Exercise

Resolve a real conflict

Two branches changed the same heading in index.html: main made it “Welcome to Academy” and feature/tagline made it “Learn to code with Academy”. Merge the feature into main, inspect the conflict with git status and git diff, and resolve it by combining both ideas into “Welcome to Academy — learn to code”. A second file, prices.txt, also conflicts: resolve it by taking the feature branch’s version whole. Finish the merge and verify there are no conflict markers left.

Terminal
mkdir conflict && cd conflict && git init -b main
echo "<h1>Academy</h1>" > index.html
echo "Pro: 10 USD" > prices.txt
git add . && git commit -qm "Create home page"

git switch -qc feature/tagline
echo "<h1>Learn to code with Academy</h1>" > index.html
echo "Pro: 12 USD" > prices.txt
git commit -qam "New tagline and prices"

git switch -q main
echo "<h1>Welcome to Academy</h1>" > index.html
echo "Pro: 9 USD" > prices.txt
git commit -qam "Friendlier heading, sale price"

# 1. Merge and look at the conflict

# 2. Resolve index.html by hand

# 3. Take the feature branch's prices.txt

# 4. Finish the merge and check

Summary

  • A conflict happens when both branches changed the same lines (or one edited a file the other deleted).
  • Git marks conflicting lines with <<<<<<<, ======= and >>>>>>>: your side first, then the incoming side.
  • Resolve by editing to the right final content, deleting all markers, then git add the file.
  • git checkout --ours/--theirs <file> takes one whole side; git merge --abort cancels the merge.
  • Small, frequent merges and short-lived branches keep conflicts small.