How to Resolve a Git Merge Conflict

A merge conflict means Git could not auto-combine two branches because they changed the same lines. You fix it by editing the file to the version you want, then committing. Here is exactly how.

Backend Engineergitmergeconflict

Why a merge conflict happens

Git combines changes from two branches automatically. When both branches changed the same lines of the same file, Git cannot decide which version to keep, so it stops and marks the file as conflicted for you to resolve. You will see it after a git merge, git pull, or git rebase:

$ git merge feature
Auto-merging main.py
CONFLICT (content): Merge conflict in main.py
Automatic merge failed; fix conflicts and then commit the result.

Read the conflict markers

Git rewrites the conflicted file with markers showing both sides:

<<<<<<< HEAD
PORT = 8080
=======
PORT = 9090
>>>>>>> feature

Resolve it in three steps

  1. Open the file and edit it to the final version you want - keep one side, the other, or combine them. Delete all three marker lines:

python PORT = 9090 TIMEOUT = 30

  1. Stage the resolved file:

bash git add main.py

  1. Finish the merge with a commit:

bash git commit --no-edit

That is it - the merge is complete.

Useful commands while resolving

git status        # files listed as "both modified" are conflicted
git diff          # shows each conflict hunk
git merge --abort # bail out and return to before the merge

How to avoid them

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

What causes a git merge conflict?

Two branches changed the same lines of the same file, so Git cannot auto-merge and asks you to choose the final version.

How do I undo a merge conflict?

Run git merge --abort (or git rebase --abort) to return to the state right before the merge started.

What do the <<<<<<< and >>>>>>> markers mean?

They wrap the two conflicting versions. HEAD is your current branch; the name after >>>>>>> is the incoming branch. Edit to the version you want and delete the three marker lines.

Keep learning

Return the Correct HTTP Status CodeBackend projectRepair Broken API PaginationBackend projectAdd CORS Middleware to an Express APIBackend projectBackend roadmapStep by step to hiredBackend interview questionsSTAR answersAll Backend projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →