How to Resolve Git Merge Conflicts, Step by Step
You run git merge to bring a teammate's branch into yours, and instead of the usual success message Git prints CONFLICT and stops. Nothing is broken: Git found two changes it cannot combine on its own and is asking you to decide. This lesson explains why merge conflicts happen, how to read the markers Git writes into your file, and how to resolve and finish the merge.
Why merge conflicts happen
To combine two branches, Git compares each one with the merge base, the newest commit both branches share. Where only one branch changed part of a file, Git takes that change; where both made the identical change, Git takes it once. Separate parts of a file usually combine cleanly, but changes on neighbouring lines can still clash.
A merge conflict is what Git reports when it cannot choose for you. The two common causes:
- Both branches changed the same lines in different ways, like the sugar line. Git calls this a content conflict.
- One branch deleted a file that the other edited. Git reports
CONFLICT (modify/delete), leaves the edited version in your folder, and you decide whether the file stays.
Here is the first kind. You are on main and merge Ravi's branch, feature, which changed the sugar line in recipe.txt that you changed too:
git merge feature
Git merges what it can, then stops and names the file that needs you:
Auto-merging recipe.txt
CONFLICT (content): Merge conflict in recipe.txt
Automatic merge failed; fix conflicts and then commit the result.
The merge is paused, with no merge commit yet, and everything that combined cleanly is already in place. git pull can stop the same way, because it merges too. The diagram shows the five steps from here to a finished merge.
Find the file and read the markers
Run git status to find every file that needs you. It lists them under Unmerged paths, files Git could not finish merging, marked both modified, with hints for the next steps:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: recipe.txt
no changes added to commit (use "git add" and/or "git commit -a")
Now open the file. Where the branches disagree, Git has written both versions, fenced by conflict markers, lines that start with seven identical characters. Read them top to bottom, as the diagram shows:
<<<<<<< HEADstarts your version.HEADmarks the branch you are on, as What Is Git? explained; here,main.=======separates the two versions.>>>>>>> featureends their version, labelled with the branch you merged in.
If you merge main into your own branch instead, your branch is HEAD and main is the incoming side. Editors help: VS Code calls the sides Current and Incoming and offers buttons such as Accept Both Changes.
Resolve: choose the content, delete the markers, add and commit
Resolving means making the file say what it should after both changes: keep your side, keep theirs, or write a combination. When both sides add something, such as two new ingredients, the answer is often both. Here neither number is obviously right, so ask Ravi why he cut the sugar.
Say you agree on 1 spoon. Edit the file to hold only the final text, with all three marker lines deleted. Lines 2 to 4 now read:
Water: 1 cup
Sugar: 1 spoon
Milk: 1 cup
Then tell Git the file is resolved: git add stages its new content and marks it resolved. Git does not check the content, so a file still holding markers is accepted too. When every conflicted file is added, commit:
git add recipe.txt
git commit
git commit opens your editor with a ready-made message, Merge branch 'feature'; save and close it to create the merge commit. If any file is still unmerged, Git refuses (trimmed):
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
Run git status to find the file you missed. The rm in the hint is for modify/delete conflicts: git rm deletes a file and stages the deletion. The flowchart shows the whole loop.
Check before you commit, or start over
Removing the markers only makes the file look finished. Your chosen lines must work with the rest of the code, and so must the changes Git merged on its own: if Ravi renamed a function your branch still calls, nothing conflicts, yet the program breaks. So run the project's tests, if it has them, before git commit; testing has its own lessons later in this course. With no tests, as with recipe.txt, re-read each resolved file in full, not just the lines you chose.
To catch leftover markers, run this after git add. It checks your staged changes for marker lines and for whitespace errors, such as spaces at the end of a line:
git diff --cached --check
No output means it found nothing. A missed marker is reported with its file and line number, for example:
recipe.txt:3: leftover conflict marker
If the merge is going badly, git merge --abort puts your branch and files back as they were before git merge. It prints nothing when it works. The catch: changes you had not committed before merging may not come back, so the git merge documentation recommends committing or stashing (setting aside) your work first.
A rebase can stop with the same markers. Finish each file with git add, then run git rebase --continue, or git rebase --abort to start over. The sides are swapped: HEAD is main plus your commits replayed so far.
Avoid painful merge conflicts
Conflicts are a normal part of teamwork, but these habits keep them small and rare:
- Keep branches small and short-lived. A branch that lasts days touches fewer lines and drifts less from
main, as Branches and Pull Requests explained. - Bring in
mainoften. Merge the latestmaininto your branch every day or so, and you meet each conflict while it is one line, not twenty. - Talk to the other author. Agree who changes what in a shared file, and when a conflict comes, ask what their side meant instead of guessing.
- Keep reformatting separate. Re-indenting a whole file changes every line and clashes with everyone's work, so commit it on its own and warn the team.
Check yourself
You fixed recipe.txt and ran git add, but git commit still says you have unmerged files. What is wrong?
Another file is still conflicted. Every conflicted file must be resolved and added before the merge can be committed; git status lists the rest under Unmerged paths.
Both branches added a different ingredient at the same spot. Should you keep the HEAD side or the incoming side?
Usually both. Each line is a separate, valid addition, so picking one side would silently drop a teammate's work.
Your file has no markers left. Why run the tests before you commit the merge?
Markers only show where lines clashed. Your chosen lines and the changes Git merged on its own can still break the program together, and tests catch that.