Resolving Merge Conflicts
Recognize when conflicts happen, read conflict markers, resolve and commit the result, abort a merge safely and prevent most conflicts in the first place.
Welcome back!
Step 1 of 14Learn
Two people, one line
Ana and Ben both work on an online store. On her branch, Ana changes the shipping line on the pricing page to Free shipping on orders over $50. On his branch, Ben changes the same line to Free shipping on orders over $40, following a different email from marketing.
Ana's branch merges first. When Ben merges main into his branch, Git can't decide which sentence is right. It isn't a bug or a failure; it's Git refusing to guess about a business decision. It stops and reports:
Auto-merging pricing.html
CONFLICT (content): Merge conflict in pricing.html
Automatic merge failed; fix conflicts and then commit the result.This is a merge conflict. It sounds alarming the first few times, but it's routine on any active team, and resolving one is a mechanical, three-step process. Nothing has been lost: both versions are right there in the file. Git has only paused the merge halfway, waiting for a person to say what the final text should be. In this case, that means Ben checking with marketing about the threshold, which no tool could do for him.
Step 2 of 14Learn
When conflicts happen, and when they don't
Git merges line by line. It produces a conflict only when it can't combine the changes automatically:
- Both branches changed the same lines (or lines right next to each other) in different ways.
- One branch modified a file that the other deleted.
- Both branches added a different file with the same name.
Git does not conflict when branches change different files, or different, separate sections of the same file. Ana editing the footer while Ben edits the header of the same page merges cleanly. That's why most merges on a healthy team need no attention at all.
If both branches made the exact same change to a line, there's no conflict either, because both sides agree.
Conflicts aren't limited to git merge. You can meet them during git pull (which merges), git rebase, git stash pop and git revert. The markers and the fix are the same in every case.
Binary files such as images can conflict too. Git can't show line differences for them, so you choose one version entirely: git checkout --ours logo.png or git checkout --theirs logo.png, then add the file.
Step 3 of 14Diagram
Conflict? Follow the change
Tap a shape with to see more.
- Same lines changed?
If the two branches touched different lines, Git merges automatically.
- Changed differently?
Identical edits on both sides merge cleanly.
- Git pauses the merge
Files get conflict markers and are listed as unmerged.
- You decide the result
Edit, remove markers,
git add, thengit commit.
Step 4 of 14Practice · Choose one
Which of these merges will produce a merge conflict?
Step 5 of 14Learn
Reading conflict markers
Open the conflicted file and you'll find a section like this:
<h2>Shipping</h2>
<<<<<<< HEAD
<p>Free shipping on orders over $40</p>
=======
<p>Free shipping on orders over $50</p>
>>>>>>> main
<p>Delivery in 3-5 business days.</p>The markers divide the conflict into two sides:
- From
<<<<<<< HEADto=======is your current branch's version (HEAD, the branch you're on, here Ben's). - From
=======to>>>>>>> mainis the incoming version, from the branch being merged in, labeled with its name.
Everything outside the markers merged cleanly and needs no attention.
To resolve, edit the file so it contains exactly what the final version should be, and delete all three marker lines. You can keep your side, keep theirs, combine them, or write something new. If marketing confirms $40, Ben's final text is just:
<h2>Shipping</h2>
<p>Free shipping on orders over $40</p>
<p>Delivery in 3-5 business days.</p>Editors such as VS Code highlight conflicts and offer buttons like Accept Current Change, Accept Incoming Change and Accept Both Changes, which do this editing for you. Use them, but always read the result.
Step 6 of 14Practice · Diagram
Reading a conflict block
Which part separates the two versions and must be deleted with the other markers?
Tap the shape that answers the question, then press Check.
- Conflict block
- <<<<<<< HEAD
Start marker. What follows is your current branch's version.
- Your version
Free shipping over $40 (the branch you are on).
- =======
Divider between the two versions.
- Incoming version
Free shipping over $50 (the branch being merged in).
- >>>>>>> main
End marker, naming the incoming branch.
Step 7 of 14Practice · Spot the mistake
Ben thinks he has resolved this conflict and is about to commit. Click every line that must still be removed.
Step 8 of 14Learn
Finishing, or backing out
After editing every conflicted file:
- Run
git status. Conflicted files are listed under Unmerged paths. - Mark each resolved file with
git add pricing.html. For Git, adding a file is how you say "this conflict is resolved." - When all files are added, run
git commit. Git pre-fills a merge message; save it, and the merge is complete.
Before committing, search the project for leftover markers. A quick check is git diff --check, which flags conflict markers and whitespace problems, or simply search your editor for <<<<<<<. Then run the tests or open the page, because a file can be free of markers and still be wrong, for example if you kept a function call from one side and deleted the function from the other.
Getting out. If the conflict is bigger than expected, or you need to ask someone first, you can cancel the merge completely:
git merge --abort
This returns your branch and files to exactly how they were before you started the merge. The equivalents for other operations are git rebase --abort and git cherry-pick --abort. Knowing that a clean escape exists takes a lot of the stress out of conflicts.
Step 9 of 14Diagram
Resolving, start to finish
Tap a shape with to see more.
- git status
Lists conflicted files under Unmerged paths.
- Edit each file
Pick or combine the versions; delete all markers.
- git add file
Marks that file as resolved.
- git commit
Completes the merge.
- Or: git merge --abort
Backs out completely, as if you never started the merge.
Step 10 of 14Practice · Put in order
Put the steps for resolving a merge conflict in order.
Drag the items, or use the arrow buttons, to put them in order.
- Delete the conflict markers and save the file
- git add pricing.html
- git merge reports CONFLICT in pricing.html
- Open pricing.html and decide the final text with your teammate
- git commit to complete the merge
Step 11 of 14Note
Step 12 of 14Practice · What would you do?
What would you do?
During a merge, a conflict appears in checkout.js. You don't understand the incoming change, the author is offline until tomorrow, and you have other work to do today. What's the best action?
Step 13 of 14Practice · True or false
In a conflict block, the section between <<<<<<< HEAD and ======= is the version from the branch you are currently on.
Step 14 of 14Practice · Choose one
You resolved the conflicts in two files and saved them. git commit still says "Committing is not possible because you have unmerged files." What did you miss?
Lesson complete!