Skip to content
FR
  1. LuzineDebug
  2. Git & GitHub
  3. Auto-merging src/app.js
Git & GitHub Beginner ≈ 15 min to fix

Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git

Error message
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.
In short

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.

Be the first to confirm this fix

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

  1. Same lines changed on both sides (CONFLICT (content)): two people edited the same function or the same config line.
  2. File deleted on one side, modified on the other (CONFLICT (modify/delete)).
  3. Same file created on both sides (CONFLICT (add/add)), often README.md or .gitignore.
  4. Generated files under version control: package-lock.json, composer.lock or build outputs modified by each branch.
  5. Branch kept away from main for a long time: the more it diverges, the more conflicts you get.

Step-by-step fix

  1. List the conflicted files

    Conflicted files appear under "Unmerged paths" (both modified, deleted by them…). git status also tells you whether you are in a merge, a rebase or a cherry-pick.

    bash
    git status
    git diff --name-only --diff-filter=U
  2. Read the markers

    Between <<<<<<< HEAD and =======: your current version. Between ======= and >>>>>>> feature: the incoming version. Keep one, the other or a combination, then delete the three marker lines.

    plaintext
    <<<<<<< HEAD
    const port = 3000;
    =======
    const port = process.env.PORT || 8080;
    >>>>>>> feature
  3. Resolve with a tool

    VS Code offers "Accept Current Change", "Accept Incoming Change", "Accept Both Changes" and a three-way merge editor. git mergetool opens the configured tool. To keep one side of a file entirely, use --ours or --theirs; careful, during a rebase these two words are swapped (ours = the branch you are rebasing onto).

    bash
    git mergetool
    # keep one whole side of a file
    git checkout --ours -- src/app.js
    git checkout --theirs -- src/app.js
  4. Check that no marker is left

    git diff --check reports leftover conflict markers. Also run your tests or at least the build: a file with no marker can still be inconsistent.

    bash
    git diff --check
    npm test
  5. Mark as resolved and finish

    git add marks each file as resolved (git rm for a deleted file you do not want to keep). Then finish the current operation.

    bash
    git add src/app.js
    # merge or pull
    git commit
    # rebase
    git rebase --continue
    # cherry-pick
    git cherry-pick --continue
  6. 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.

    bash
    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 main into 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, and rerere.enabled true so 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

All Git & GitHub errors →