A merge conflict occurs when Git cannot automatically combine changes from different branches. This commonly happens when two branches modify the same part of a file in different ways.
A merge conflict happens when Git cannot determine automatically how to combine changes from two different development histories.
For example, two developers may edit the same line of a file differently.
Branch A → Change X
Branch B → Change Y
Git needs a developer to decide which final content should remain.
Conflicts commonly happen when different branches make incompatible changes to the same part of a file.
Suppose the main branch contains:
<h1>Welcome to My Website</h1>
The feature branch changes the same line to:
<h1>Welcome to Our Website</h1>
If another branch has changed that same line differently, Git may not be able to merge the changes automatically.
A simple workflow that can lead to a conflict is:
git switch main git switch -c feature-oneChange a particular line and commit it. Then another branch can modify the same line differently.
A conflict is usually discovered when you merge one branch into another:
git switch main
git merge feature-one
If Git cannot automatically combine the changes, the merge stops and Git reports the conflicting files.
Use git status after Git reports a conflict.
git status
Git will identify files that contain unresolved conflicts.
Git places special markers in conflicted files.
<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-login
These markers show the two versions that Git could not automatically combine.
The line:
<<<<<<< HEAD
marks the beginning of the content from the branch currently checked out during the merge.
The separator:
=======
separates the current branch's content from the incoming branch's content.
The ending marker identifies the incoming branch:
>>>>>>> feature-login
The branch name may be different depending on the branch being merged.
<<<<<<< HEAD
Welcome to My Website
=======
Welcome to Our Website
>>>>>>> feature-homepage
The current branch contains one version, while the incoming branch contains another version.
To resolve the conflict, edit the file and decide what the final content should be.
For example, you might keep:
Welcome to Our Website
Or you may create a completely different final version.
After deciding the correct content, remove all conflict markers:
<<<<<<<
=======
>>>>>>>
The final file should contain only the content that you want to keep.
After resolving a conflict, stage the file:
git add index.html
Staging the file tells Git that you have resolved its conflict.
Run:
git status
Git should show the resolved file as staged and no longer list it as an unresolved conflict.
After resolving all conflicts and staging all affected files, complete the merge:
git commit
Git can use a merge commit message that you can review and edit.
git merge feature-login
# Conflict occurs
git status
# Edit conflicted files
git add .
git commit
This is the basic workflow for resolving a merge conflict.
If you do not want to continue the merge, you can abort the merge while it is in progress:
git merge --abort
Git attempts to return the repository to the state before the merge started.
A single merge can produce conflicts in multiple files.
git status
Review every conflicted file, resolve each one, and stage each resolved file.
git add index.html
git add style.css
git add script.js
After carefully resolving every conflicted file, you can stage all resolved changes:
git add .
Always verify the result with git status before completing
the merge.
<<<<<<< HEAD
<h1>My Website</h1>
=======
<h1>Our Website</h1>
>>>>>>> feature-title
After resolving:
<h1>Our Website</h1>
Then stage the file:
git add index.html
<<<<<<< HEAD
font-size: 20px;
=======
font-size: 24px;
>>>>>>> feature-style
Choose the required value and remove the conflict markers.
font-size: 24px;
<<<<<<< HEAD
let message = "Hello";
=======
let message = "Welcome";
>>>>>>> feature-message
Choose or combine the required code and remove the conflict markers before staging the file.
Git can also work with external merge tools configured on your computer.
git mergetool
A merge tool can provide a graphical or specialized interface for comparing and resolving conflicting versions.
Conflicts cannot always be avoided, but good development practices can reduce unnecessary conflicts.
git merge --abort after already completing the merge.git merge branch-name
git status
git diff
git add filename
git add .
git commit
git merge --abort
git mergetool
# Switch to target branch
git switch main
# Start merge
git merge feature-login
# Check conflicts
git status
# Open and resolve conflicted files
# Stage resolved files
git add .
# Check status
git status
# Complete merge
git commit
git switch main
git merge feature-homepage
# CONFLICT occurs
git status
# Edit index.html
# Remove conflict markers
# Keep the correct final content
git add index.html
git status
git commit -m "Resolve homepage merge conflict"
git log --oneline --graph --all
After the conflict is resolved and committed, the two development histories have been successfully combined.
A merge conflict occurs when Git cannot automatically combine changes from different branches.
git status
git add .
git commit
The basic process is to inspect the conflicted files, decide what the final content should be, remove conflict markers, stage the resolved files, and complete the merge.
If you decide not to continue the merge, use:
git merge --abort
git status shows conflicted files.<<<<<<<,
=======, and >>>>>>>.git add filename to mark a file as resolved.git commit to complete the merge.git merge --abort to cancel a merge in progress.git mergetool can be used with a configured merge tool.Question: What should you normally do after resolving a merge conflict in a file?