How to Resolve a Merge Conflict in Git

Find the files Git could not merge, edit out the conflict markers, stage the fixed files, and finish the merge or rebase with the right command.

A git merge, git pull, or git rebase stopped with "CONFLICT (content)". Git has written both versions into the affected files and is waiting for you to choose.

Find the conflicting files

git status
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   src/config.py

Every file listed under "Unmerged paths" needs attention.

Open each file and find the markers

Search for <<<<<<<. Each conflict looks like this:

<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> feature/slow-api

The block between <<<<<<< HEAD and ======= is the version on your current branch. The block between ======= and >>>>>>> is the version being merged in. During a rebase the labels are swapped: HEAD is the branch you are rebasing onto, and the lower block is your own commit.

Edit the file to its final content

Replace the whole block, markers included, with what the code should say. That might be one side, the other, or a combination:

timeout = 60

Delete all three marker lines. Repeat for every conflict in the file, then for every file in the list.

Stage the resolved files

git add src/config.py

Run git status again. A file moves from "Unmerged paths" to "Changes to be committed" once it is staged, which is how you tell Git the conflict is resolved. Git does not check that the markers are gone, so search the file for <<<<<<< before staging if you are unsure.

Finish the merge or rebase

For a merge (including one started by git pull):

git commit

Git opens an editor with a prefilled merge message. Save and close it.

For a rebase:

git rebase --continue

If later commits in the rebase also conflict, Git stops again and you repeat the steps.

If you want to back out instead. git merge --abort or git rebase --abort returns the branch to the state before you started, with none of the conflict edits kept.

If you use an editor with Git support. VS Code and most IDEs highlight each conflict and show Accept Current Change, Accept Incoming Change, and Accept Both Changes buttons. They edit the same markers for you; you still run git add and finish the merge on the command line.

More Coding how-tos