Skip to main content

Merge Conflicts

A merge conflict happens when two branches change the same part of the same file, and Git cannot decide which version to keep. Git stops the merge and asks you to resolve it manually.

Conflicts look scary the first time. They are not — once you understand the markers, they are straightforward to resolve.


Create a Conflict on Purpose

The best way to understand conflicts is to make one deliberately.

# Start on main
git switch main

# Edit a line
echo "Line from main branch" > conflict-demo.txt
git add conflict-demo.txt
git commit -m "Add conflict-demo from main"

# Create a branch off the same commit
git switch -c conflict-branch

# Edit the same file differently
echo "Line from conflict-branch" > conflict-demo.txt
git add conflict-demo.txt
git commit -m "Add conflict-demo from branch"

# Switch back to main and try to merge
git switch main
git merge conflict-branch

Git will stop with:

Auto-merging conflict-demo.txt
CONFLICT (content): Merge conflict in conflict-demo.txt
Automatic merge failed; fix conflicts and then commit the result.

Read the Conflict Markers

Open conflict-demo.txt:

conflict-demo.txt<<<<<<< HEAD↑ your current branch (main)Line from main branch=======↑ separatorLine from conflict-branch>>>>>>> conflict-branch

Three zones separated by the markers:

ZoneMeaning
Between <<<<<<< HEAD and =======Your version (current branch)
Between ======= and >>>>>>> <branch>Incoming version (the branch being merged)

Resolve the Conflict

Edit the file to the version you want. You have three options:

Keep your version

Line from main branch

Keep the incoming version

Line from conflict-branch

Combine both

Line from main branch and conflict-branch combined

Delete all three marker lines (<<<<<<<, =======, >>>>>>>). The file should contain only the final content you want, with no markers remaining.


Finish the Merge

git add conflict-demo.txt
git commit

Git opens your editor with a pre-filled merge commit message. Save and close the editor to complete the merge.

Run git log --oneline --graph to see the merge commit:

* e4f5a6b (HEAD -> main) Merge branch 'conflict-branch'
|\
| * 9c8d7e6 Add conflict-demo from branch
* | 3b2a1f0 Add conflict-demo from main
|/
* a1b2c3d Initial commit

Run Status During a Conflict

git status is especially useful during a merge conflict:

git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")

Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: conflict-demo.txt

Files listed under Unmerged paths still have unresolved conflicts. You must git add each one after resolving to mark it as done.


Abort a Merge

If you started a merge and want to get back to the state before:

git merge --abort

This cancels the merge entirely and returns both branches to where they were.


Summary

WhatHow
Conflict markers<<<<<<< / ======= / >>>>>>>
ResolveEdit the file, remove all markers
Mark resolvedgit add <file>
Complete mergegit commit
Cancel mergegit merge --abort
Donate to this project