Merge Conflicts
About
In Git, a merge is the process of integrating changes from one branch into another. It typically happens when:
We have been working on a feature branch, and now we want to bring our changes into
main
(or any other base branch).Multiple team members have worked on different branches, and those branches are now being combined.
A merge can be done with:
git merge <branch>
Git tries to automatically reconcile differences, but if changes affect the same lines or structure in both branches, merge conflicts arise.
What Is a Merge Conflict?
A merge conflict occurs when Git cannot automatically determine which changes to keep and which to discard. This happens when both branches modify the same part of a file differently, or if there's a structural conflict (e.g., file moved or deleted in one branch and modified in another).
Git then stops the merge and asks us to manually resolve the conflicts.
Common Causes of Merge Conflicts
Same Line Edited Differently Both branches made changes to the same line(s) in the same file.
One Deleted, One Modified One branch deleted a file while the other modified it.
File Renamed or Moved Differently File paths diverge between branches, confusing Git on how to reconcile.
Complex Structural Changes If both branches restructure large chunks of the same file or class.
What Happens Internally ?
When we run:
git merge feature-branch
Git:
Locates the common ancestor of the current and target branches.
Calculates changes from the ancestor to each branch.
Tries to apply both sets of changes together.
If overlapping changes are detected, Git cannot auto-resolve and marks the affected files as conflicted.
How Git Shows Merge Conflicts ?
Git inserts conflict markers in the conflicted files like this:
<<<<<<< HEAD
Our branch’s changes
=======
Incoming branch’s changes
>>>>>>> feature-branch
We must edit the file manually to choose or combine changes, then remove the markers.
How to Resolve a Merge Conflict ?
Run the merge:
git merge feature-branch
Git pauses on conflict, and marks conflicting files.
Open the conflicted files, and manually:
Review both versions
Keep the correct lines from each version
Delete the conflict markers
Stage the resolved files:
git add <file>
Complete the merge:
git commit
Git does not auto-create a commit if a conflict occurred. We must commit after resolving.
Optional Commands
Abort the merge and return to the previous state:
git merge --abort
View conflicts:
git status
Use visual tools:
IDEs (IntelliJ, VSCode, Eclipse) often provide a merge conflict editor.
CLI tools like
git mergetool
can be configured.
Last updated
Was this helpful?