Merge Conflicts
A conflict means two branches changed the same lines differently. Git can’t guess which is right, so it asks you.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Nov 25, 2026
Learning objectives#
- Recognise when a conflict happens
- Read conflict markers
- Resolve conflicts in VS Code
- Finish the merge with add and commit
Explanation#
Git merges most changes automatically. A conflict only happens when both branches edited the same lines (or one deleted a file the other edited).
Git marks the conflicting part in the file: <<<<<<< HEAD starts your version, ======= separates, >>>>>>> other-branch ends their version. The file is not usable until you remove the markers.
VS Code shows buttons above each conflict: Accept Current Change, Accept Incoming Change, Accept Both Changes, Compare Changes. Pick one or edit by hand, save, then stage the file and commit.
On GitHub, a PR with conflicts shows “This branch has conflicts that must be resolved” and a Resolve conflicts button for simple cases.
Step-by-step screens#
Drawings of github.com, the Git installer, VS Code and a terminal. GitHub changes its layout from time to time; the button names stay the same. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: A conflict and its fix#
git switch feature/new-title
git merge main
# CONFLICT (content): Merge conflict in index.html
# index.html now contains:
# <<<<<<< HEAD
# <h1>Welcome to my portfolio</h1>
# =======
# <h1>Hello, I'm Asha</h1>
# >>>>>>> main
# Keep the line you want, delete the markers, save, then:
git add index.html
git commit -m "Merge main into feature/new-title"
Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. [feature/new-title 4d5e6f7] Merge main into feature/new-title
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Merging main into the branch reveals the conflict early.
- HEAD is the branch you are on; below ======= is main’s version.
- Edit the file to the final text and remove all three markers.
- git add marks it resolved; git commit finishes the merge.
Real-world use case#
Two teammates both edit the page title. The second to merge resolves the conflict in VS Code in under a minute and the PR turns green again.
Common mistakes#
Leaving <<<<<<< in the file breaks the page. Search for <<<<<<< before committing.
git merge --abort returns to the state before the merge.
Best practices#
- Pull or merge main into your branch often
- Keep branches short-lived
- Talk to the teammate who made the other change
- Run the app after resolving
Remember this#
<<<<<<< yours ======= theirs >>>>>>>
Edit, save, add, commit.
git merge --abort to undo.
Practice exercise#
Your task: Cancel a merge that went wrong and return to the state before it.
Show solution
git merge --abort
Mini quiz#
When does Git report a conflict?
Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions
My notes#
Saved in this browser.