The short answer
#Git could not combine two changes to the same lines, so it has paused the merge and written both versions into the file. Open each conflicted file, edit it so it contains the code you actually want, remove the conflict markers, then stage the file and finish the merge.
_10git status # lists files under "Unmerged paths"_10# edit each file and remove the markers_10git add src/app.js_10git commit # or: git merge --continue
If it all goes wrong, git merge --abort puts everything back to how it was before you started.
Why it happens
#Git merges line by line. When one branch changed some lines and the other branch left them alone, Git takes the change automatically. When both branches changed the same lines, or one branch edited a file the other deleted, Git has no way to know which version is right. It stops and asks you.
Inside the file, the conflict looks like this:
_10<<<<<<< HEAD_10const timeout = 5000;_10=======_10const timeout = 10000;_10>>>>>>> feature/retry
The part between <<<<<<< and ======= is your current branch (HEAD). The part between ======= and >>>>>>> is the branch you are merging in. The same thing happens during git rebase, git cherry-pick and git stash pop, though during a rebase the meaning of "ours" and "theirs" is swapped, because Git is replaying your commits on top of the other branch.
How to fix it
#Edit the file by hand
This is the most reliable approach. For each conflict block, decide what the final code should be. That might be one side, the other side, or a mix of both. Delete all three marker lines, save, and check that the code still builds and the tests pass.
_10git diff --name-only --diff-filter=U # list files that still have conflicts_10git add src/config.js_10git merge --continue
Search the file for <<<<<<< before staging. Git will happily commit a file that still contains markers.
Take one side for a whole file
When you know one version of a file is correct, for example a lockfile you will regenerate anyway, you can take it wholesale:
_10git checkout --ours package-lock.json # keep your branch's version_10git checkout --theirs package-lock.json # keep the incoming version_10git add package-lock.json
For lockfiles it is usually better still to take either side and then run your package manager's install command, so the lockfile matches the merged package.json.
Use a merge tool
Your editor probably has a built-in conflict view. VS Code, JetBrains IDEs and most Git GUIs show the two sides with buttons to accept either or both. You can also launch a configured tool from the terminal:
_10git mergetool
Show the original too
The default markers only show the two new versions, which makes it hard to see what each side actually changed. Setting the conflict style to zdiff3 adds a third section with the common ancestor:
_10git config --global merge.conflictStyle zdiff3
Now you can see the original line and work out the intent of each change, which makes the right answer much easier to spot.
Start again
If you get muddled, abandon the merge and try again from a clean state:
_10git merge --abort # or: git rebase --abort
Common pitfalls
#- Committing a file that still contains conflict markers. Search for them, or let your linter or test suite catch the syntax error before you push.
- Keeping both sides by default. Two versions of the same function or two imports of the same thing often compile but behave wrongly.
- Forgetting that "ours" and "theirs" are reversed during a rebase.
- Resolving a lockfile by hand. Regenerate it instead.
- Starting a merge with uncommitted changes. Commit or stash first so you are only dealing with one set of changes.
- Not running the code afterwards. A merge can be free of conflicts and still be broken, so build and test before you push.
Related reading
#Gitquick question
Was this resource helpful?