How to Resolve a Git Merge Conflict
A step-by-step guide to resolving a Git merge conflict, using a real conflict created from scratch, with the output at each step and how to cancel safely.
Short answer: to resolve a Git merge conflict, open each conflicted file, choose what the final lines should be (and delete the <<<<<<<, ======= and >>>>>>> markers), then run git add on the file and git commit. Git is not broken when this happens: two branches changed the same lines and Git is asking you to decide. Below is a real conflict, created and fixed step by step.
Why does a merge conflict happen?
When you merge, Git combines the changes from two branches. It handles most of this on its own. A conflict appears only when both branches changed the same lines of the same file (or one branch edited a file the other deleted). Git cannot know which version you want, so it stops and marks the spot. If you are new to branches, start with Git and GitHub explained.
How do you create a merge conflict to practise on?
Try this in an empty folder. It makes a file, then changes the same line on two branches:
git init -b main
printf 'Welcome to Lakeview Cafe\nOpen daily: 9am to 5pm\nMenu: tea, coffee\n' > menu.txt
git add . && git commit -m "Add menu"
git switch -c summer-hours
sed -i 's/9am to 5pm/9am to 8pm/' menu.txt
git commit -am "Extend hours for summer"
git switch main
sed -i 's/9am to 5pm/10am to 6pm/' menu.txt
git commit -am "Change opening hours"(sed -i is the Linux command; on macOS use sed -i '', or just edit the line by hand.) Now merge the other branch:
git merge summer-hoursGit replies:
Auto-merging menu.txt
CONFLICT (content): Merge conflict in menu.txt
Automatic merge failed; fix conflicts and then commit the result.How do you read the conflict markers?
Run git status -sb. The file is listed as UU, meaning "unmerged on both sides". Open menu.txt:
Welcome to Lakeview Cafe
<<<<<<< HEAD
Open daily: 10am to 6pm
=======
Open daily: 9am to 8pm
>>>>>>> summer-hours
Menu: tea, coffeeMarker | Meaning |
|---|---|
| Start of your version: the branch you are on (main) |
| The divider between the two versions |
| End of the incoming version from the branch you merged |
How do you resolve it step by step?
Decide what the line should say. You can keep yours, keep theirs, or combine both. Here we want the later opening time and the longer closing time.
Edit the file so only the final text remains, and remove all three marker lines:
Welcome to Lakeview Cafe Open daily: 10am to 8pm Menu: tea, coffeeMark it resolved:
git add menu.txtFinish the merge:
git commit -m "Merge summer-hours: keep 10am to 8pm"
Check the result with git log --oneline --graph:
* 9c0f835 Merge summer-hours: keep 10am to 8pm
|\
| * fcb2285 Extend hours for summer
* | c9a217f Change opening hours
|/
* 9f4ac50 Add menu(Your commit IDs will differ.) Both branches now join in one merge commit.
Can you cancel a merge that went wrong?
Yes. While the merge is still unfinished, run git merge --abort. It returns your branch to how it was before you started. Nothing is lost.
How do you avoid conflicts?
Pull the latest changes often (
git pull) so branches do not drift far apart.Keep branches short-lived and changes small.
Agree in a team who edits which files, and avoid mass reformatting while others work.
Use a merge tool or your editor's conflict view: it shows both sides and lets you click to accept one.
Common mistakes
Committing with the markers still inside the file. Search for
<<<<<<<before you commit.Always picking "mine" without reading the other side, which silently throws away a teammate's work.
Resolving the text but forgetting to run the tests. A clean merge can still break the code.
To practise more, follow the tutorial chapter on branches, merging and merge conflicts.
Key Takeaway
A merge conflict is just Git asking you to choose. Read the markers, write the final text, then git add and git commit. If you get stuck, git merge --abort takes you back safely.
Ready to start? You will practise branches, merging and conflict resolution with real exercises. Begin with the free course Git, APIs and Backend for AI: Beginner.
Related articles
More in APIs and Backend
What Is a Pull Request? A Beginner's Guide to Code Review
A pull request is a proposal to merge one branch into another, reviewed by teammates first. Learn the step-by-step cycle, a description template, what reviewers check and the three merge methods.
6 min readDec 2, 2026What Is a Webhook? Webhooks vs API Polling
A webhook is an HTTP request a service sends to your URL when an event happens. Compare webhooks with API polling and learn to verify webhook signatures in Python.
5 min readNov 30, 2026What Is Docker and Why Do AI Developers Use It?
Docker packs your app and everything it needs into a container that runs the same everywhere. Learn images, containers and Dockerfiles, and why AI developers rely on them.
4 min readNov 23, 2026FastAPI vs Flask vs Django for AI Apps
Which Python web framework should you use for an AI app? A plain comparison of FastAPI, Flask and Django, when each fits, and a tiny working endpoint.
4 min readNov 19, 202615 Essential Git Commands (Cheat Sheet)
The 15 Git commands you will use almost every day, each with a one-line purpose and an example, plus a daily workflow and common mistakes.
4 min readNov 17, 2026How to Keep API Keys Safe: .env Files and Secrets
Keep API keys out of your code: use .env files and environment variables, ignore them in Git, and know exactly what to do if a key leaks.
4 min readNov 14, 2026
Comments
Sign in to join the discussion.

