Git Error: Automatic merge failed; fix conflicts and then commit the result.
Educational use only. Content explains errors and defensive fixes for systems you own or are authorised to test. Do not use any technique here to access data, accounts, or networks without permission.
Root Cause
This happens when Git is unable to automatically integrate changes from two different branches. It typically occurs when two developers have modified the exact same lines of code in the same file, or when one developer deleted a file while another modified it. Git stops the merge process to prevent data loss and injects conflict markers (<<<<<<<, =======, >>>>>>>) into the affected files. The codebase is left in a 'merging' state. Trying to run code with these markers will result in syntax errors. The developer must manually review these conflicting sections, decide which code to keep (or write a new combination of both), remove the conflict markers, and finalize the merge.
Fix / Solution
Open the files with conflicts (listed by `git status`). Search for the `<<<<<<<` markers. Carefully evaluate the changes from both `HEAD` (your current branch) and the incoming branch. Edit the file to reflect the desired final state, ensuring you delete all Git conflict markers. Then, stage the resolved files with `git add` and complete the merge with `git commit`.
Code Snippet
// ❌ Broken (File with Conflict Markers)
<<<<<<< HEAD
const port = process.env.PORT || 8080;
=======
const port = 3000; // Hardcoded for local testing
>>>>>>> feature/local-dev
// ✅ Fixed (Resolved File)
const port = process.env.PORT || 3000;
// Terminal Commands to Finalize
# git add server.js
# git commit -m "Fix merge conflict in server.js"