Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Terminal Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Merge Conflicts


Share

What is a Merge Conflict?

A merge conflict happens when two branches change the same part of a file.

Git can't decide which change to keep, so you have to choose.

You must resolve the conflict before you can finish the merge.


Why Do Merge Conflicts Happen?

Merge conflicts usually happen when you merge branches that changed the same lines in a file.

This is common in collaborative projects or when working on long-lived branches.


How to See and Resolve Merge Conflicts

When you merge a branch and there are conflicting changes, Git will pause and mark the files with conflicts.

Example: Merge a Branch

git merge feature-branch
Auto-merging filename.txt
CONFLICT (content): Merge conflict in filename.txt
Automatic merge failed; fix conflicts and then commit the result.
Try it Yourself »

If there are conflicts, Git will tell you which files are affected.


See Which Files Have Conflicts

Use git status to see which files need your attention:

Example: Check Status

git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   filename.txt

no changes added to commit (use "git add" and/or "git commit -a")
Try it Yourself »


See the Differences

Use git diff to see what changed and help you decide how to resolve the conflict:

Example: See Differences

git diff
diff --cc filename.txt
index 56f2c83,0c6be20..0000000
--- a/filename.txt
+++ b/filename.txt
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
 +Your changes here
++=======
+ Other branch's changes
++>>>>>>> feature-branch
Try it Yourself »

Edit the Conflict Markers

Open the conflicted file. You'll see sections like this:

Conflict Markers

<<<<<<< HEAD
Your changes here
=======
Other branch's changes
>>>>>>> feature-branch

Edit the file to keep what you want, then remove the conflict markers (<<<<<<<, =======, >>>>>>>).


Mark as Resolved

After fixing the file, mark it as resolved:

Example: Mark Resolved

git add filename.txt
Try it Yourself »

Complete the Merge

Finish the merge with a commit (if Git doesn't do it automatically):

Example: Finish Merge

git commit
Try it Yourself »

Cancel the Merge

If you want to stop and undo the merge:

Example: Abort Merge

git merge --abort
Try it Yourself »

Use a Visual Merge Tool

If you prefer, you can use a visual tool to resolve conflicts:

Example: Use Mergetool

git mergetool

Pick One Side's Changes

If you want to keep only your changes or only the other branch's changes:

Example: Keep Our Changes

git checkout --ours filename.txt
Try it Yourself »

Example: Keep Their Changes

git checkout --theirs filename.txt
Try it Yourself »

Troubleshooting & Best Practices

  • If you're stuck, you can always use git merge --abort to start over.
  • Make sure you remove all conflict markers before marking as resolved.
  • If you use git mergetool and don't like the result, you can still edit the files by hand.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.