Git Conflict Resolution: Resolve Merge Conflicts
Learn how to identify, understand, and resolve git merge conflicts. Step-by-step guide with conflict markers, strategies, and tools.
git mergetoolDetailed Explanation
What Are Merge Conflicts?
Merge conflicts occur when Git cannot automatically reconcile differences between two branches. This happens when the same lines in the same file were modified differently in each branch.
Conflict Markers
When a conflict occurs, Git inserts markers into the affected file:
<<<<<<< HEAD
const apiUrl = "https://api.production.com";
=======
const apiUrl = "https://api.staging.com";
>>>>>>> feature/new-api
<<<<<<< HEAD: Your current branch's version=======: Separator>>>>>>> feature/new-api: The incoming branch's version
Resolving Step-by-Step
- Identify conflicted files:
git status
# Shows "both modified" files
Open each file and choose the correct version (or combine both).
Remove conflict markers — delete the
<<<<<<<,=======, and>>>>>>>lines.Stage the resolved file:
git add src/config.ts
- Continue the merge or rebase:
git merge --continue
# or
git rebase --continue
Using a Merge Tool
# Configure a merge tool (e.g., VS Code)
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Launch the tool
git mergetool
Aborting a Merge
If you want to start over:
git merge --abort
# or
git rebase --abort
Prevention Strategies
- Merge main into your branch frequently to reduce drift.
- Keep commits small and focused — large commits are harder to merge.
- Communicate with teammates when working on the same files.
- Use code ownership (CODEOWNERS file) to reduce overlapping changes.
Conflicts are a natural part of collaboration. The key is resolving them promptly and correctly rather than avoiding them entirely.
Use Case
Two developers modified the same configuration file on separate branches. During the pull request merge, a conflict arises that needs manual resolution before the merge can complete.