In this guide, we’ll explain why merge conflicts happen, how to identify them, and the steps to resolve them. By mastering conflict management, you can ensure successful merges and maintain a clean codebase.

What Causes Merge Conflicts?

Merge conflicts occur when:

  • Two branches modify the same line in a file.
  • One branch deletes a file while another modifies it.
  • File renames or moves overlap between branches.

For example, if two developers edit the same function in a file, Git cannot determine which change to keep.

Detecting Merge Conflicts

When you attempt to merge branches with conflicting changes, Git stops the merge and marks the affected files. You’ll see an error message like:

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

Use git status to list files with conflicts:

git status

Files with conflicts will be marked as both modified.

Steps to Resolve Merge Conflicts

Follow these steps to resolve conflicts:

1. Open the Conflicting File

Git marks the conflicting areas with:

<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> branch-name

The section between <<<<<<< HEAD and ======= contains your changes. The section between ======= and >>>>>>> branch-name contains changes from the other branch.

2. Edit the File

Manually edit the file to keep the desired changes and remove the conflict markers. For example:

// Resolved version of file.txt
This is the desired content after resolving conflicts.

3. Mark the Conflict as Resolved

After editing, stage the file to mark the conflict as resolved:

git add 

4. Complete the Merge

Commit the resolved changes to complete the merge:

git commit

Example: Resolving Conflicts in a .NET Project

Suppose two branches modify the same function in Program.cs:

// Version in main branch
Console.WriteLine("Hello from main!");
// Version in feature-login branch
Console.WriteLine("Hello from feature-login!");

When merging feature-login into main, Git reports a conflict. Open the file, and Git shows:

<<<<<<< HEAD
Console.WriteLine("Hello from main!");
=======
Console.WriteLine("Hello from feature-login!");
>>>>>>> feature-login

Edit the file to resolve the conflict:

// Resolved version
Console.WriteLine("Hello from main and feature-login!");

Then stage and commit the resolved file:

git add Program.cs
git commit -m "Resolve merge conflict in Program.cs"

Using Tools to Resolve Conflicts

Many IDEs and tools simplify conflict resolution, including:

  • Visual Studio: Provides a graphical interface for resolving conflicts in code.
  • VS Code: Highlights conflicts and offers quick actions to resolve them.
  • Third-party tools: Tools like Meld and Beyond Compare are popular for resolving conflicts visually.

Best Practices for Avoiding Conflicts

  • Pull frequently: Regularly update your branch with the latest changes from the main branch.
  • Communicate: Coordinate with your team to avoid overlapping changes.
  • Small commits: Break your work into smaller, more frequent commits.

Conclusion

Merge conflicts are an inevitable part of collaborative development, but they don’t have to be intimidating. By understanding why they occur and following structured steps to resolve them, you can ensure a smooth workflow. Use tools and best practices to minimize conflicts and keep your project moving forward.