Resolving Merge Conflicts in Git: Step-by-Step Guide
In this tutorial, you'll learn about Resolving Merge Conflicts in Git: Step. We cover key concepts, practical examples, and best practices.
A merge conflict occurs when Git's automatic merge fails because both branches changed the same lines, pausing to mark regions needing manual resolution.
In this tutorial, you'll learn what Git merge conflicts are and how to resolve them step by step. Merge conflicts happen when two branches modify the same part of a file — Git can't decide which change to keep and asks you to choose. By the end, you'll handle conflicts confidently using both command-line tools and graphical merge tools, and you'll know how to reduce conflicts in your team workflow.
flowchart TD
A[git merge feature] --> B{Conflict?}
B -->|No| C[Auto-merge complete]
B -->|Yes| D[Git marks conflicted files]
D --> E[Open files, find conflict markers]
E --> F[Choose version or combine]
F --> G[git add resolved files]
G --> H[git commit to complete merge]
Understanding Conflict Markers
When a conflict occurs, Git inserts markers in the affected file:
<<<<<<< HEAD
This is the current branch version.
=======
This is the incoming branch version.
>>>>>>> feature/login
<<<<<<< HEAD— start of the current branch's version=======— division between the two versions>>>>>>> feature/login— end of the incoming branch's version
You must edit the file to remove the markers and keep the correct code.
Step-by-Step Conflict Resolution
Create a conflict deliberately to practice:
# Setup
mkdir conflict-demo && cd conflict-demo
git init
echo "line 1\nline 2\nline 3" > file.txt
git add file.txt
git commit -m "Initial commit"
# Create two branches that modify the same line
git checkout -b branch-a
echo "line 1\nline A\nline 3" > file.txt
git add file.txt
git commit -m "Branch A change"
git checkout main
git checkout -b branch-b
echo "line 1\nline B\nline 3" > file.txt
git add file.txt
git commit -m "Branch B change"
# Merge branch-a into branch-b to create a conflict
git checkout branch-b
git merge branch-a
Expected output:
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
Now open file.txt and resolve:
# After resolving, the file should look like:
echo "line 1\nline A and B\nline 3" > file.txt
git add file.txt
git commit -m "Resolve merge conflict between branch-a and branch-b"
Using Git Merge Tools
For complex conflicts, use a graphical merge tool:
# Configure a diff tool
git config merge.tool vimdiff
git config merge.conflictstyle diff3
# Launch the merge tool
git mergetool
The diff3 conflict style adds the merge base (the common ancestor) as a third section, making it easier to understand what both sides changed:
<<<<<<< HEAD
line B
||||||| merged common ancestors
line 2
=======
line A
>>>>>>> branch-a
Aborting a Merge
If a conflict becomes too messy, abort and start fresh:
git merge --abort
This returns your working directory to the state before the merge started.
Preventing Merge Conflicts
| Practice | Why It Helps |
|---|---|
| Pull before pushing | Reduces divergence with remote |
| Short-lived branches | Less time for conflicts to accumulate |
| Frequent rebase on main | Keeps your branch close to the latest code |
| Clear code ownership | Teams know who to coordinate with |
| Small, focused commits | Easier to understand what changed |
| Communication | Tell teammates when modifying shared files |
Common Errors
| Error | Cause | Fix |
|---|---|---|
CONFLICT (content) |
Both branches changed same lines | Resolve manually and git add |
CONFLICT (rename/rename) |
Both branches renamed same file | Decide which name to keep |
CONFLICT (delete/modify) |
One branch deleted, other modified | Choose to keep or delete |
CONFLICT (add/add) |
Both branches added same-named file | Merge contents manually |
Automatic merge failed |
Git cannot resolve differences | Use git mergetool or manual resolution |
Merge made by the 'ort' <a href="/design-patterns/strategy/">strategy</a> |
Success (not an error) | Verify with git log |
| Staged changes disappeared | Merge aborted | git stash before merging |
| Untracked files overwritten | File exists locally but not in either branch | Back up then merge |
Practice Questions
Challenge
Create a repository with three files. On main, edit the first line of each file. Create branch feature-x and edit the last line of each file. Create branch feature-y and edit both first and last lines. Merge feature-x into feature-y, then merge feature-y into main, resolving all conflicts. Write the complete command sequence.
Real-World Task
In a shared team repository, pull the latest main, create a feature branch, and make changes to a configuration file that you know another developer is also editing. Before merging, run git pull --rebase origin main to catch conflicts early. Resolve any conflicts, complete the rebase, and push. This exact scenario happens daily at DodaTech where multiple teams contribute to Durga Antivirus Pro's configuration system.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro