Merge conflicts occur in Git when two or more changes to the same file cannot be merged automatically. These conflicts usually happen when multiple developers modify overlapping lines of code in the same file. Understanding how to resolve these conflicts ensures smooth collaboration in a Git-based workflow.
Why Do Merge Conflicts Happen?
A merge conflict arises when:
- Two branches modify the same line in a file differently.
- A file is deleted in one branch but modified in another.
- A file is edited in two branches without a clear way to merge the changes automatically.
Identifying Merge Conflicts
When Git encounters a conflict, it stops the merge process and marks the conflicting files. You will see an error message like this:
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
How to Resolve Merge Conflicts
When Git encounters a merge conflict, it pauses the merge and marks the conflicting files. To resolve conflicts, check the affected files using git status
, open them, and manually edit the conflicting sections. Remove conflict markers (<<<<<<<
, =======
, >>>>>>>
) and choose the correct changes. After editing, stage the resolved files with git add
, then commit them using git commit
. If you were rebasing, continue with git rebase --continue
. This ensures a smooth resolution of conflicts and allows you to complete the merge or rebase successfully.
Step 1: Check the Conflicted Files
Run the following command to list files with merge conflicts:
git status
It will display files that need manual resolution under the section:
both modified: index.html
Step 2: Open the Conflicted File
Git marks the conflicting section in the file using special markers:
<<<<<<< HEAD
<p>This is the current version in the main branch.</p>
=======
<p>This is the new version from the feature branch.</p>
>>>>>>> feature-branch
Here:
<<<<<<< HEAD
indicates the version in the current branch (e.g.,main
).=======
separates the two versions.>>>>>>> feature-branch
shows the conflicting changes from the incoming branch.
Step 3: Resolve the Conflict
Manually edit the file to keep the correct version. You can choose one change or merge both.
Option 1: Keep the current branch's change
<p>This is the current version in the main branch.</p>
Option 2: Keep the incoming branch's change
<p>This is the new version from the feature branch.</p>
Option 3: Merge both changes
<p>This is the current version in the main branch. This is the new version from the feature branch.</p>
Step 4: Mark the Conflict as Resolved
After editing the file, remove the conflict markers (<<<<<<<
, =======
, >>>>>>>
) and save the file.
Step 5: Add and Commit the Resolved File
Run the following commands:
git add index.html
git commit -m "Resolved merge conflict in index.html"
Step 6: Complete the Merge
If you were merging a branch, the process is now complete. However, if you were rebasing, you must continue the rebase process.
Understanding Rebasing vs. Merging
- Merging (
git merge
): Combines two branches, creating a new commit that preserves both histories. - Rebasing (
git rebase
): Moves your branch's commits to the latest version of the target branch, keeping a linear history.
What to Do If You Were Rebasing
If you were using git rebase
and encountered a conflict, resolve it manually as shown earlier. Then, instead of committing normally, run:
git rebase --continue
If more conflicts appear in later commits, Git will stop again. Resolve them step by step until the rebase completes. If you want to cancel the rebase and keep your branch unchanged, use:
git rebase --abort
This restores the branch to its previous state before starting the rebase.
Using a Merge Tool for Conflict Resolution
Git provides built-in and third-party merge tools like vimdiff
, meld
, and kdiff3
. To use a merge tool:
git mergetool
Git will open the merge tool for manual resolution. After saving the changes, commit them as shown earlier.
Preventing Merge Conflicts
- Pull changes frequently: Before making changes, update your branch using
git pull origin main
. - Work in small commits: Committing frequently makes conflict resolution easier.
- Use feature branches: Isolate new changes to avoid conflicts in the
main
branch. - Communicate with team members: Coordinate changes to avoid modifying the same file simultaneously.
Conclusion
In this tutorial, you learned how to identify, resolve, and prevent merge conflicts in Git. You now know how to use git status
to find conflicts, manually edit files to merge changes, and finalize the merge with git add
and git commit
. We also covered using git mergetool
for easier resolution and best practices like frequent pulls and feature branches. Mastering these steps ensures smooth collaboration and efficient conflict management in Git.