Git merge conflicts occur when Git cannot automatically combine changes from different branches. This usually happens when the same part of a file is modified in multiple branches. Understanding how to detect, analyze, and resolve merge conflicts is a critical skill for collaborative development.
git merge or git pullA merge conflict happens when Git cannot decide which change to keep. Git marks the conflicting areas directly inside the file using special markers so developers can manually fix them.
// Git reports a conflict during merge
CONFLICT (content): Merge conflict in file-name
// Git conflict markers inside a file
<<<<<<< HEAD
Changes from the current branch
=======
Changes from the incoming branch
>>>>>>> feature-branch
// Show files with conflicts
git status
// Mark file as resolved
git add file-name
// Commit resolved merge
git commit -m "Resolved merge conflict in file-name"
Once conflicts are resolved and committed, Git completes the merge and the branch history becomes consistent again.
Create two branches, modify the same line in a file, and merge them to see conflict markers in action.
// Simulate conflict scenario
git checkout -b branch-one
git checkout -b branch-two
git mergetool for visual resolution