← Back to Chapters

Git Merge Conflicts

⚔️ Git Merge Conflicts

? Quick Overview

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.

? Key Concepts

  • Merge conflicts occur during git merge or git pull
  • Git pauses the merge until conflicts are resolved
  • Conflicts must be fixed manually by the developer
  • Resolved files must be staged and committed

? Syntax / Theory

A 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.

? Detecting a Merge Conflict

? View Code Example
// Git reports a conflict during merge
CONFLICT (content): Merge conflict in file-name

? Conflict Markers Explained

? View Code Example
// Git conflict markers inside a file
<<<<<<< HEAD
Changes from the current branch
=======
Changes from the incoming branch
>>>>>>> feature-branch

?️ Resolving Merge Conflicts

  1. Check repository status
? View Code Example
// Show files with conflicts
git status
  1. Edit conflicted files and remove markers
  2. Stage resolved files
? View Code Example
// Mark file as resolved
git add file-name
  1. Commit the merge
? View Code Example
// Commit resolved merge
git commit -m "Resolved merge conflict in file-name"

? Live Output / Explanation

What Happens?

Once conflicts are resolved and committed, Git completes the merge and the branch history becomes consistent again.

? Interactive Example

Create two branches, modify the same line in a file, and merge them to see conflict markers in action.

? View Code Example
// Simulate conflict scenario
git checkout -b branch-one
git checkout -b branch-two

? Use Cases

  • Team-based software development
  • Parallel feature development
  • Long-running branches
  • Open-source contributions

? Tips & Best Practices

  • Pull frequently to stay updated
  • Keep commits small and focused
  • Communicate with teammates
  • Test code after resolving conflicts

? Try It Yourself

  • Create conflicting changes across branches
  • Resolve conflicts manually
  • Use git mergetool for visual resolution