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.
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
- From
<<<<<<< HEADto=======is your branch's version. - From
=======to>>>>>>> featureis the incoming branch's version.
Resolve it in three steps
- 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
- Stage the resolved file:
bash
git add main.py
- 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
- Pull or rebase often so branches do not drift far apart.
- Keep changes small and focused on one area.
- For big conflicts, use
git mergetoolor your editor's built-in conflict view.
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
- Reading conflict markers and resolving them by hand
- Finishing a merge with git add and commit
- Aborting a merge cleanly when you want to start over
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
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 →