How to Merge Conflicts in Git: A Comprehensive Guide

Merge conflicts are an inevitable part of working with Git, especially when collaborating on projects with multiple contributors. A merge conflict occurs when Git is unable to automatically merge changes from two branches because both branches have conflicting changes to the same part of a file. However, resolving merge conflicts is a skill every Git user should master to maintain a smooth and efficient workflow.

In this guide, we’ll walk you through the steps to identify, resolve, and commit merge conflicts in Git.


❓ What Is a Merge Conflict?

A merge conflict happens when:

  • Two branches have made changes to the same line in the same file.
  • One branch has made changes to a file that has been deleted in another branch.

Git cannot decide which changes should take precedence, so it marks the file as conflicted and requires manual intervention.


✅ How to Identify a Merge Conflict

When you attempt to merge two branches that have conflicting changes, Git will notify you with a message like this:

Automatic merge failed; fix conflicts and then commit the result.

To see which files have conflicts, run:

git status

You’ll see output similar to this:

both modified:   filename

This means the file filename has conflicts that need to be resolved.


✅ How to Resolve Merge Conflicts

🔹 Step 1: Open the Conflicted File

Git marks conflicted files with conflict markers like:

<<<<<<< HEAD
Your changes (current branch)
=======
Changes from the branch you're merging
>>>>>>> branch-name
  • The section between <<<<<<< HEAD and ======= represents the changes in your current branch.
  • The section between ======= and >>>>>>> branch-name represents the changes from the branch you’re merging.

🔹 Step 2: Edit the File

You need to manually resolve the conflict by choosing either:

  • Keeping your changes
  • Keeping the incoming changes
  • Combining both sets of changes logically

After deciding how to resolve the conflict, remove the conflict markers (<<<<<<<, =======, >>>>>>>) from the file.

🔹 Step 3: Stage the Resolved File

Once you’ve resolved the conflicts in the file, you need to stage the file for commit:

git add filename

You can stage multiple files if necessary:

git add .

🔹 Step 4: Commit the Merge

After staging the resolved files, commit the merge:

git commit -m "Resolved merge conflict in filename"

Git will create a commit with a default message indicating that it was a merge.


🛠 Tools for Resolving Merge Conflicts

While you can always resolve merge conflicts manually in your text editor, there are tools that can make the process easier:

  • VS Code: Provides a graphical interface to view and resolve conflicts.
  • GitKraken: A Git GUI tool with a visual conflict resolution interface.
  • Sourcetree: Another Git GUI tool that simplifies conflict resolution.
  • git mergetool: A command-line tool that opens an external merge tool for conflict resolution.

To use git mergetool, first, make sure you have a merge tool configured:

git config --global merge.tool meld  # Or your preferred tool

Then run:

git mergetool

✅ How to Abort a Merge (If Necessary)

If you encounter a situation where you no longer wish to resolve the conflict, you can abort the merge and return to the state before the merge began:

git merge --abort

For rebases, the command is:

git rebase --abort

🧠 Tips for Preventing Merge Conflicts

  • Pull frequently: Regularly pulling changes from the main branch helps avoid long-running divergences.
  • Keep changes small: Make small, focused commits, so it’s easier to resolve conflicts.
  • Use feature branches: Work in feature branches and only merge them back to the main branch when they are complete.

✅ Summary

TaskCommand
Check for merge conflictsgit status
Resolve conflicts manuallyEdit file, remove conflict markers
Stage resolved filegit add filename
Commit the mergegit commit -m "Resolved conflict"
Abort a mergegit merge --abort
Use mergetool (optional)git mergetool

🚀 Final Thoughts

Merge conflicts are an inevitable part of collaborative workflows, but knowing how to resolve them effectively is key to a smooth development process. By staying organized, using the right tools, and communicating with your team, you can minimize the chances of conflicts and handle them swiftly when they occur.

Sharing Is Caring:

Leave a Comment