Handling Merge Conflicts in Git – A Simple Guide
What is a Merge Conflict?
A merge conflict happens when Git doesn’t know which changes to keep because two branches have modified the same part of a file. Think of it like two people editing the same sentence in a document – Git needs your help to decide which version to keep.
But don’t worry! Merge conflicts are easy to resolve when you follow the right steps.
Why Do Merge Conflicts Happen?
✅ Two branches modify the same line in a file.
✅ One branch deletes a file while the other edits it.
✅ A file is modified in different ways on both branches.
How to Detect Merge Conflicts
When you try to merge two branches, Git will attempt to combine them automatically. If there’s a conflict, you’ll see a message like this:
git merge feature-branch
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
To check which files have conflicts:
git status
Git will list the files that need attention. Now let’s fix them!
️ How to Resolve Merge Conflicts (Step-by-Step)
1️⃣ Open the Conflicting File
Git marks conflicting parts of the file like this:
<<<<<<< HEAD
This is the content from the main branch.
=======
This is the content from the feature branch.
>>>>>>> feature-branch
Here’s what each part means:
- Between
<<<<<<< HEAD
and=======
→ Code from your current branch. - Between
=======
and>>>>>>> feature-branch
→ Code from the branch being merged.
2️⃣ Choose Which Changes to Keep
Now, decide which version to keep or combine both:
<p>This is the final merged content.</p>
Once you've made your changes, save the file.
3️⃣ Mark the Conflict as Resolved
After editing, tell Git the conflict is fixed by staging the file:
git add <file-name>
Example:
git add index.html
4️⃣ Complete the Merge
Now, create a merge commit:
git commit -m "Resolved merge conflict in index.html"
Done! You’ve successfully handled a merge conflict.
⚡ Pro Tips for Avoiding Merge Conflicts
Merge conflicts can be frustrating, but they’re easily avoidable with good Git habits. Follow these tips to minimize conflicts and keep your workflow smooth:
- Pull the latest changes before you start coding to ensure you’re working with the most up-to-date version.
git pull origin main
- Communicate with your team if multiple people are working on the same files to prevent overlapping changes.
- Use feature branches to keep new work isolated and prevent conflicts with
main
. - Commit often in smaller chunks, making it easier to resolve issues when conflicts arise.
By following these best practices, you’ll encounter fewer merge conflicts and resolve them quickly when they do occur.
Need to Cancel a Merge?
If things go wrong and you want to stop the merge, you can abort it:
git merge --abort
This will undo the merge and restore your branch to its previous state.
Next Steps
Now that you know how to handle merge conflicts, explore more Git topics:
Collaborating with GitHub – Learn about pull requests and team workflows.
Collaborating with GitLab – Manage merge requests and DevOps workflows.
Need help? Check the official Git documentation or drop a question in the comments!