Merge conflicts · Git and GitHub
What do you do when two changes collide?
Course contentsWhy you need it
When two people change the same lines, Git can't know which version is right, so it stops and asks you. Conflicts are normal on any team, not a sign that something went badly wrong, and resolving one calmly takes a minute once you know the steps.
The intuition
Git marks the clash inside the file:
<<<<<<< HEAD
your version (the branch you are on)
=======
their version (the branch you are merging)
>>>>>>> new-titleThe steps are always:
git status: which files conflict?- Edit each file: keep one side, the other, or a mix, and delete the marker lines.
git add FILEto mark it solved.git committo finish the merge.
Changed your mind? git merge --abort puts everything back as before the merge.
Definition
Merge conflict
Both branches changed the same part of a file. Git stops the merge until you choose the result.
Conflict markers
The lines <<<<<<<, ======= and >>>>>>> that Git writes into a file to show both versions. They must be removed before you commit.
Examples
Meet a conflict
Run it in the lessonmain renamed the shop, new-title renamed it differently. Merge, and look.
git merge new-title
git status
cat README.md- L1CONFLICT: Git stops halfway.
- L2README.md is listed as both modified.
- L3Both versions, between the markers.
<<<<<<< HEAD
Resolve it
Run it in the lessonWrite the final version, mark it solved, commit.
git merge new-title
echo "# My Best Shop" > README.md
git add README.md
git commit -m "merge new-title"
git log --oneline- L2The final text, with no markers left.
- L3add means: this conflict is solved.
- L4The merge commit completes the merge.
Common mistakes
Committing the markers
If <<<<<<< is still in the file, you've committed a broken file. Search for it before git add.
Keeping only your side by reflex
The other change was made for a reason. Read both sides; often the right answer combines them.
Quick quiz
1. You fixed the file. What tells Git the conflict is resolved?
Show the answer
Answer: A
Staging the fixed file marks it resolved; then commit.
2. What does the last command print?
git merge new-title
git merge --abort
cat README.mdShow the answer
Answer: A
--abort returns to exactly how things were before the merge: main's version.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictRead the markers
After this merge, which line between the markers comes from the branch
new-title? - ModifyFinish the merge
This resolves the conflict but forgets two steps, so the merge never finishes. Add them so the merge is committed with the message
merge new-title. - WriteKeep both ideas
Merge
new-titleinto main and resolve the conflict so README.md contains exactly the single line# My Best Shop. Commit the merge.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Merge conflict | 合并冲突 | Two branches changed the same lines; you choose the result. |
| Abort | 中止 | Cancel an operation and go back to before it started. |