Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git
Auto-merging src/app.js CONFLICT (content): Merge conflict in src/app.js Automatic merge failed; fix conflicts and then commit the result.
- CONFLICT (modify/delete): src/old.js deleted in HEAD and modified in feature. Version feature of src/old.js left in tree.
- CONFLICT (add/add): Merge conflict in README.md
- error: could not apply a1b2c3d... Update app.js hint: Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue".
- error: Pulling is not possible because you have unmerged files.
- fatal: Exiting because of an unresolved conflict.
Both branches changed the same lines and Git cannot tell which one to keep. Open each conflicted file, pick the right content between the <<<<<<<, ======= and >>>>>>> markers, remove the markers, then git add and git commit; git merge --abort cancels everything.
What this error means
During a merge, pull, rebase or cherry-pick, Git compares each file with their common ancestor. If only one branch changed an area, it applies the change automatically. If both changed the same lines, or one deleted a file the other modified, Git cannot decide on its own: it writes both versions into the file between markers, marks the file as "unmerged" and pauses the operation. It is not a failure, it is a request for a decision. Until the conflicts are resolved and marked with git add, Git refuses to commit or pull again.
Common causes
- Same lines changed on both sides (
CONFLICT (content)): two people edited the same function or the same config line. - File deleted on one side, modified on the other (
CONFLICT (modify/delete)). - Same file created on both sides (
CONFLICT (add/add)), oftenREADME.mdor.gitignore. - Generated files under version control:
package-lock.json,composer.lockor build outputs modified by each branch. - Branch kept away from
mainfor a long time: the more it diverges, the more conflicts you get.
Step-by-step fix
-
List the conflicted files
Conflicted files appear under "Unmerged paths" (
both modified,deleted by them…).git statusalso tells you whether you are in a merge, a rebase or a cherry-pick.git status git diff --name-only --diff-filter=U -
Read the markers
Between
<<<<<<< HEADand=======: your current version. Between=======and>>>>>>> feature: the incoming version. Keep one, the other or a combination, then delete the three marker lines.<<<<<<< HEAD const port = 3000; ======= const port = process.env.PORT || 8080; >>>>>>> feature -
Resolve with a tool
VS Code offers "Accept Current Change", "Accept Incoming Change", "Accept Both Changes" and a three-way merge editor.
git mergetoolopens the configured tool. To keep one side of a file entirely, use--oursor--theirs; careful, during a rebase these two words are swapped (ours = the branch you are rebasing onto).git mergetool # keep one whole side of a file git checkout --ours -- src/app.js git checkout --theirs -- src/app.js -
Check that no marker is left
git diff --checkreports leftover conflict markers. Also run your tests or at least the build: a file with no marker can still be inconsistent.git diff --check npm test -
Mark as resolved and finish
git addmarks each file as resolved (git rmfor a deleted file you do not want to keep). Then finish the current operation.git add src/app.js # merge or pull git commit # rebase git rebase --continue # cherry-pick git cherry-pick --continue -
Cancel everything if needed
If you got lost, go back to the state before the operation and start again calmly. The resolution work in progress will be discarded.
git merge --abort git rebase --abort git cherry-pick --abort
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Bring
maininto your branch often (git pull --rebase origin main) and keep pull requests short. - Enable
git config --global merge.conflictStyle zdiff3(Git 2.35+) to also see the original version in conflicts, andrerere.enabled trueso Git reapplies resolutions you already made. - For a conflicted lock file, do not merge it by hand: take one version, then regenerate it (
npm install,composer update --lock).
Frequently asked questions
What is the difference between ours and theirs?
During a merge, ours is your current branch and theirs is the branch being merged. During a rebase, it is the opposite: ours is the base branch and theirs is your replayed commits.
I committed a file that still contains <<<<<<<, what now?
Fix the file, then make a new commit. If that commit is not pushed yet, you can use git commit --amend instead.
How do I resolve a conflict directly on GitHub?
On the pull request, the "Resolve conflicts" button opens a web editor for simple conflicts. For deleted files, binaries or large conflicts, do it locally.
Useful tools for this error
More Git & GitHub errors
- Fix "failed to push some refs" (! [rejected] fetch first) in Git
- Fix "fatal: refusing to merge unrelated histories" in Git
- Fix "Permission denied (publickey)" with Git and GitHub
- Get out of "detached HEAD" state in Git without losing commits
- Fix "Your local changes to the following files would be overwritten by merge" in Git
- Fix "Support for password authentication was removed" on GitHub
- Fix "GH001: Large files detected" (file over 100 MB) on GitHub