Resolving merge conflicts
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
git merge feature/taglineAuto-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:
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.txtReading conflict markers
Git writes both versions into the file, with markers around them:
<<<<<<< HEADto=======: 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
- Open the file and decide what the final content should be. It might be your side, their side, a combination, or something new.
- Delete all three marker lines. A leftover
<<<<<<<will break your code (and is embarrassing in a pull request). - Mark it resolved by staging it:
git add index.html. - When every conflicted file is staged, finish the merge:
git commit # Git pre-fills the message: "Merge branch 'feature/tagline'"
# or
git merge --continueTaking one side
When you know one whole version is right:
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:
git config --global merge.conflictStyle zdiff3Now conflicts (in Git 2.35 and later) include a middle section showing the original:
<<<<<<< HEAD
<h1>Welcome to Academy</h1>
||||||| 1a2b3c4
<h1>Academy</h1>
=======
<h1>Learn to code with Academy</h1>
>>>>>>> feature/taglineNow 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:
git merge --abortEverything 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 mergetoolopens 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
mainkeep 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.
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.
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 checkmkdir 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
git merge feature/tagline || true # CONFLICT (content): Merge conflict in index.html …
git status # both modified: index.html, prices.txt
git diff # shows the conflict markers
# 2. Resolve index.html by hand (in an editor you'd delete the markers)
echo "<h1>Welcome to Academy — learn to code</h1>" > index.html
git add index.html
# 3. Take the feature branch's prices.txt
git checkout --theirs prices.txt
git add prices.txt
# 4. Finish the merge and check
git commit --no-edit # or: git merge --continue
grep -rn '<<<<<<<\|>>>>>>>' . --include='*.html' --include='*.txt' || echo "no markers left"
git log --oneline --graphSummary
- 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 addthe file. git checkout --ours/--theirs <file>takes one whole side;git merge --abortcancels the merge.- Small, frequent merges and short-lived branches keep conflicts small.