Resolve Git Merge Conflicts Step by Step
A merge conflict happens when two branches changed the same lines of the same file in different ways. Git cannot know which version is right, so it stops and asks you. Conflicts are normal on any shared project and they are not dangerous: nothing is lost, and you can always back out.
What Git does
When a merge hits a conflict, Git:
- Merges every file it can automatically.
- Prints
CONFLICT (content): Merge conflict in <file>for each problem file. - Writes both versions into those files, between conflict markers.
- Leaves the merge unfinished, waiting for you.
git status now lists the files under "Unmerged paths" as "both modified".
Reading the markers
<<<<<<< HEAD
The meeting is on Tuesday.
=======
The meeting is on Thursday.
>>>>>>> reschedule
Everything between <<<<<<< HEAD and ======= is your side, the branch you are on. Everything between ======= and >>>>>>> reschedule is the side being merged in. Lines outside the markers merged cleanly.
Resolving
- Open the file and decide what the result should be: one side, the other, or a combination.
- Delete all three marker lines and leave the final text.
git addthe file to mark it as resolved.git committo finish the merge (Git prepares a message for you).
In the transcript, both branches edit the same line. The merge stops with a conflict, cat shows the markers, and then we write the agreed text, stage it and commit. Editors such as VS Code highlight conflict blocks and offer "accept current", "accept incoming" or "accept both" buttons that do step 2 for you.
git init -q
echo "The meeting is on Monday." > plan.txt
git add plan.txt
git commit -q -m "Plan meeting"
git switch -q -c reschedule
echo "The meeting is on Thursday." > plan.txt
git commit -q -am "Move to Thursday"
git switch -q main
echo "The meeting is on Tuesday." > plan.txt
git commit -q -am "Move to Tuesday"
git merge reschedule
git status --short
cat plan.txt
echo "The meeting is on Thursday at 10am." > plan.txt
git add plan.txt
git commit -q --no-edit
git log --oneline --format="%s" Transcript (verified by our test suite in a fresh temp folder)
$ git init -q $ echo "The meeting is on Monday." > plan.txt $ git add plan.txt $ git commit -q -m "Plan meeting" $ git switch -q -c reschedule $ echo "The meeting is on Thursday." > plan.txt $ git commit -q -am "Move to Thursday" $ git switch -q main $ echo "The meeting is on Tuesday." > plan.txt $ git commit -q -am "Move to Tuesday" $ git merge reschedule Auto-merging plan.txt CONFLICT (content): Merge conflict in plan.txt Automatic merge failed; fix conflicts and then commit the result. $ git status --short UU plan.txt $ cat plan.txt <<<<<<< HEAD The meeting is on Tuesday. ======= The meeting is on Thursday. >>>>>>> reschedule $ echo "The meeting is on Thursday at 10am." > plan.txt $ git add plan.txt $ git commit -q --no-edit $ git log --oneline --format="%s" Merge branch 'reschedule' Move to Tuesday Move to Thursday Plan meeting
Backing out
If a conflict is bigger than you expected, git merge --abort puts everything back the way it was before you started the merge. Nothing from either branch is lost. You can then talk to the person who wrote the other change, or merge main into your branch in smaller steps.
Making conflicts rarer
- Merge or pull from
mainoften, so branches do not drift far apart. - Keep commits and branches small and focused.
- Avoid reformatting whole files in the same branch as real changes; formatting touches every line and collides with everyone.
Common mistakes
- Committing a file that still contains
<<<<<<<markers. Search for them beforegit add. - Picking one side blindly. Read both: often the right answer combines them.
Summary
- Conflicts mean both sides changed the same lines; Git pauses the merge.
- Edit the file, remove the markers,
git add, thengit commit. git merge --abortsafely cancels.
Quiz: Merge conflicts