Why Conflicts Happen
A merge combines what each branch changed since their merge base. When the two branches changed different parts of a file, or different files, Git combines them on its own. When both changed the same lines in different ways, Git cannot know which result you want. It stops, marks the disputed lines in the file and asks you to decide. That is a merge conflict.
A conflict is not an error or damage. Nothing is lost: both versions are in the file, and the merge simply waits for your decision.
What Git Shows You
Git reports the conflict as it happens:
Auto-merging uploads.txt
CONFLICT (content): Merge conflict in uploads.txt
Automatic merge failed; fix conflicts and then commit the result.
git status then says You have unmerged paths. and lists each conflicted
file under "Unmerged paths" as both modified. While the merge is unfinished,
Git will not let you switch branches.
Inside the file, the conflict looks like this:

<<<<<<< HEADstarts the conflict. The lines after it are your current branch's version, heremain's.=======divides the two versions.- The lines after it are the other branch's version, ending at
>>>>>>> add-gif, which names the branch you are merging. - Everything outside the markers merged without trouble.
Resolving a Conflict
Resolving means editing the file so it says what the project should say, then telling Git you are done:
-
Decide the content. Read both versions and work out the intent of each. The answer might be one side, the other, or a combination. Often the right result keeps both intentions.
-
Edit the file. Write the result and delete all three marker lines.
-
Check for leftover markers before you stage the file:
git diff --checkIt prints
leftover conflict markerwith the line number for any marker still in the file, and nothing when the file is clean. -
Mark it resolved by staging it:
git add <file>. When every conflicted file is staged,git statussaysAll conflicts fixed but you are still merging.To check what you staged, add--staged:git diff --staged --check -
Finish the merge:
git merge --continueGit opens your editor with the prepared message,
Merge branch '<name>'. Save and close it, and the merge commit is made.
Git does not check that your result makes sense. It accepts whatever you stage, even a file that still contains markers, which is why the checks in steps 3 and 4 matter.
Backing Out
If you would rather not merge yet, for example because you need to ask the author of the other branch, abort:
git merge --abort
Your branch and files go back to exactly how they were before the merge. Start
merges from a clean git status so that abort has nothing else to restore.
Exercise: Resolve a Conflicted Merge
Two branches changed the same settings line: add-gif allows GIF uploads, and
main meanwhile allowed WebP. Both changes are wanted.
Starting state: Git configured as in the configuration lesson.
-
Run the setup:
mkdir -p ~/git-practice/conflict-practice cd ~/git-practice/conflict-practice git init -b main cat > uploads.txt <<'EOF' # Upload settings max_size_mb = 10 allowed_types = jpg, png keep_days = 30 EOF git add uploads.txt git commit -m "Add upload settings" git switch -c add-gif cat > uploads.txt <<'EOF' # Upload settings max_size_mb = 10 allowed_types = jpg, png, gif keep_days = 30 EOF git commit -am "Allow GIF uploads" git switch main cat > uploads.txt <<'EOF' # Upload settings max_size_mb = 10 allowed_types = jpg, png, webp keep_days = 30 EOF git commit -am "Allow WebP uploads" -
Merge
add-gifintomain:git merge add-gifGit reports
CONFLICT (content): Merge conflict in uploads.txt. -
Look at the state and the file:
git status cat uploads.txtuploads.txtis listed asboth modified, and the file contains the markers shown in the diagram. -
Practise backing out, then start the merge again:
git merge --abort git status git merge add-gifAfter the abort,
git statusreports a clean working tree and the file is back tomain's version. The second merge stops at the same conflict. -
Resolve it. Both changes are wanted, so the line should allow both new types. Write the combined file, without markers, then check:
cat > uploads.txt <<'EOF' # Upload settings max_size_mb = 10 allowed_types = jpg, png, webp, gif keep_days = 30 EOF git diff --checkgit diff --checkprints nothing. In your own projects you would make this edit in your editor; the command here writes the same result. -
Mark the file resolved and check what you staged:
git add uploads.txt git status git diff --staged --checkgit statussaysAll conflicts fixed but you are still merging., and the check prints nothing. -
Finish the merge. Your editor opens with
Merge branch 'add-gif'; save and close it.git merge --continue git log --oneline --graph --all cat uploads.txtThe graph shows the merge commit joining both branches, and the file allows
jpg, png, webp, gif.
Success: git diff --check reported nothing, the merge commit exists with
two parents, and uploads.txt contains both new file types and no markers.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
leftover conflict marker | A marker line is still in the file | Delete it, then run the check again |
fatal: cannot switch branch while merging | The merge is unfinished | Finish it with git merge --continue, or git merge --abort |
git status still lists both modified | The file was not staged after editing | git add <file> |
An editor opened during git merge --continue | Git is asking for the merge commit's message | Save and close to accept it |
There is no merge in progress | The merge already finished or was aborted | Check git log --oneline --graph |
-
Clean up:
cd ~/git-practice ls rm -rf conflict-practice ls
Independent task: before cleaning up, run this setup. It adds a README and
an app name on main, then renames the app on one branch while main marks it
as a beta, so the merge conflicts in two files:
cat > README.md <<'EOF'
# Photo Share
Share photos with your family.
EOF
cat >> uploads.txt <<'EOF'
app_name = Photo Share
EOF
git add README.md uploads.txt
git commit -m "Add README and app name"
git switch -c rename
cat > README.md <<'EOF'
# Snapshots
Share photos with your family.
EOF
cat > uploads.txt <<'EOF'
# Upload settings
max_size_mb = 10
allowed_types = jpg, png, webp, gif
keep_days = 30
app_name = Snapshots
EOF
git commit -am "Rename the app to Snapshots"
git switch main
cat > README.md <<'EOF'
# Photo Share Beta
Share photos with your family.
EOF
cat > uploads.txt <<'EOF'
# Upload settings
max_size_mb = 10
allowed_types = jpg, png, webp, gif
keep_days = 30
app_name = Photo Share Beta
EOF
git commit -am "Mark the app as beta"
git merge rename
Resolve both files so that both intentions survive, check for markers and finish the merge.
Check Your Understanding
- In a conflict, someone deletes both versions and the markers, leaving no
allowed_typesline at all, then stages and commits. Git accepts it. Why is that not a resolution? - What does
git merge --abortgive you back, and when would you use it?
Answer and Reasoning
Independent task
git status lists both README.md and uploads.txt as both modified. One
branch renamed the app and the other marked it as a beta, so the combined
result is "Snapshots Beta" in both files:
cat > README.md <<'EOF'
# Snapshots Beta
Share photos with your family.
EOF
cat > uploads.txt <<'EOF'
# Upload settings
max_size_mb = 10
allowed_types = jpg, png, webp, gif
keep_days = 30
app_name = Snapshots Beta
EOF
git diff --check
git add README.md uploads.txt
git diff --staged --check
git merge --continue
Both checks print nothing, and the merge commit is Merge branch 'rename'.
Keeping only one side in either file would also complete the merge, but it
would silently drop the other branch's intention.
Understanding check
- Git only checks that the conflicted files were staged; it does not check what they say. Deleting both versions throws away both branches' changes, and here it removes a setting the application needs. A resolution must contain the content the project should have, which usually means keeping both intentions.
- It returns the branch and files to exactly their state before the merge started, with no conflict and no partial result. Use it when you cannot resolve the conflict yet, for example because you need to ask the other branch's author what they intended.