Resolve a Git merge conflict: step-by-step guide
Merge conflicts scare everyone at first. This page explains why they happen, how to detect them and how to resolve them cleanly, step by step.
Why merge conflicts happen
A conflict occurs when Git cannot automatically merge two changes. It's normal and happens to everyone.
Same file, same area
Two people modify the same lines of the same file in two different branches.
Diverging branches
The longer branches live without being synced, the higher the conflict risk.
Git cannot choose
Git doesn't know which version is correct. It asks you to decide by showing both versions.
Resolving a conflict: step by step
Here is exactly what happens when a conflict occurs and how to resolve it.
1. The conflict appears
2. You resolve it
The 5 steps to resolve a conflict
Identify the conflicting files
Type git status to see the list of files marked "both modified".
Open the file and find the markers
Look for the blocks <<<<<<<, ======= and >>>>>>>. They delimit the two versions of the code.
Choose or combine the versions
Keep the HEAD version, the branch version, or combine both. Remove the conflict markers.
Mark the file as resolved
Type git add filename to tell Git that you've resolved the conflict in this file.
Complete the merge with a commit
Type git commit to finalize the merge. Git automatically creates a merge commit message.
Tips to reduce conflicts
Sync often
Regularly run git pull or git rebase main on your branch to stay up to date.
Small branches
Short, focused branches reduce the surface area for conflicts.
Communicate
Let the team know when you're modifying a sensitive or shared file.
Review the diff first
Use git diff to anticipate potential conflict zones.
Don't be afraid
A conflict is not an error. It's Git asking for your help.
Practice
GitQuest offers conflict resolution investigations in a safe environment.
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about merge conflicts
Practice resolving conflicts risk-free
The GitQuest simulator puts you face to face with real conflicts in a safe environment.
Start practicing now