When collaborating on a codebase with multiple contributors, it’s common to encounter merge conflicts in Git. These occur when two branches have made changes to the same lines of code, or when one branch modifies a file that the other deletes.
While Git is excellent at automatically merging changes, some situations require human intervention. This guide walks you through how to detect, resolve, and remove conflicts in Git effectively.
What Is a Merge Conflict?
A merge conflict happens when Git cannot automatically reconcile differences between two branches during a merge, rebase, or cherry-pick. Git will pause the process and mark the conflicted files for you to resolve.
Common Scenarios:
- Two branches edited the same line of a file
- One branch deletes a file the other modifies
- Concurrent renaming of files or directories
Step-by-Step: How to Remove Conflicts in Git
✅ Step 1: Identify the Conflict
You’ll typically see a conflict when running:
git merge branch-name
Git will stop the merge and notify you with messages like:
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
To list all conflicted files:
git status
Look for lines like:
both modified: file.txt
✅ Step 2: Open and Inspect the Conflicted File
Open the file in your code editor or IDE. Git marks the conflicting sections like this:
<<<<<<< HEAD
Your changes in the current branch
=======
Incoming changes from the branch being merged
>>>>>>> branch-name
These markers show where the conflict is and which version belongs to which branch.
✅ Step 3: Manually Resolve the Conflict
You now need to edit the file to resolve the conflict. Choose one of the following:
- Keep your version
- Keep the incoming version
- Combine both versions
- Rewrite the section entirely
After editing, remove all the conflict markers (<<<<<<<
, =======
, >>>>>>>
).
Example:
Before:
<<<<<<< HEAD
color: blue;
=======
color: red;
>>>>>>> feature/theme
After:
color: navy; /* agreed style */
✅ Step 4: Mark the File as Resolved
Once you’ve resolved the conflict in the file:
git add file.txt
Repeat this for all conflicted files.
✅ Step 5: Complete the Merge or Rebase
If you’re merging:
git commit
If you’re rebasing:
git rebase --continue
Git will now proceed with the rest of the operation.
✅ Step 6: Push Your Changes (If Needed)
If you’re working with a remote repository:
git push origin branch-name
You may need to force push (--force
) if the conflict was resolved during a rebase.
Tips for Avoiding Merge Conflicts
- Pull often to stay updated with the main branch.
- Communicate with your team about large or overlapping changes.
- Keep commits small and focused to reduce overlapping changes.
- Use feature flags to isolate new work.
- Use rebase interactively to re-order and squash commits before merging.
Conclusion
Merge conflicts are a natural part of collaborative software development. While they can be intimidating at first, resolving them is straightforward once you understand the process. The key is to stay calm, inspect the changes carefully, and communicate with your team when necessary.
By mastering conflict resolution in Git, you’ll improve your workflow and become a more effective collaborator in any development environment.