Git by Example: Merge Conflicts
Learn how to identify and resolve merge conflicts in Git. This example shows what conflict markers look like and the steps to manually fix conflicting code.
Code
# Attempt to merge a feature branch
git merge feature-login
# Output:
# Auto-merging app.js
# CONFLICT (content): Merge conflict in app.js
# Automatic merge failed; fix conflicts and then commit the result.
# Check status to see conflicting files
git status
# Open the file and see markers:
# <<<<<<< HEAD
# console.log("Hello from Main");
# =======
# console.log("Hello from Feature");
# >>>>>>> feature-login
# After manually editing the file to remove markers...
# Stage the resolved file
git add app.js
# Complete the merge
git commit -m "Merge feature-login and resolve conflicts"Explanation
Merge conflicts occur when Git cannot automatically reconcile differences between two branches. This usually happens when the same line of code has been modified differently in both branches, or when one branch deletes a file that the other modified. Git pauses the merge process and asks you to resolve the conflict manually.
When a conflict occurs, Git modifies the affected files to include "conflict markers". These markers (<<<<<<<, =======, >>>>>>>) clearly delimit the conflicting sections. The content between HEAD and ======= is what you have on your current branch, while the content between ======= and the branch name is what is coming in.
To resolve a conflict, you must edit the file, choose which code to keep (or combine both), and delete the markers. Once the file looks correct, you stage it with git add to tell Git the conflict is resolved, and then finalize the merge with git commit.
- Conflicts happen when automatic merging fails
- Look for
<<<<<<<markers in files - Manually edit files to fix the code
- Stage and commit to finish the merge

