Resolving Merge Conflicts in Git: Step-by-Step Guide

By Maulik Paghdal

22 Dec, 2024

Resolving Merge Conflicts in Git: Step-by-Step Guide

When working on collaborative projects, Git merge conflicts are almost inevitable. They occur when multiple contributors edit the same part of a file, and Git cannot automatically determine which changes to keep. Understanding how to handle these conflicts efficiently is crucial for maintaining a clean and functional codebase.

In this guide, we’ll explore what merge conflicts are, why they happen, and how to resolve them step by step.

What Are Merge Conflicts?

A merge conflict happens when Git encounters conflicting changes during a merge operation. It occurs when:

  • Two or more developers modify the same line in a file.
  • One developer deletes a file that another developer edits.
  • Changes are made to the same file but in overlapping sections.

Git halts the merge process until conflicts are resolved, ensuring that developers review and decide on the appropriate changes.

Common Scenarios Leading to Merge Conflicts

  1. Simultaneous Updates: Multiple contributors edit the same part of a file.
  2. Feature Branch Merging: When merging a long-running feature branch with the main branch.
  3. Rebasing: Conflicts arise when trying to replay commits onto a different base.
  4. Cherry-picking: Selecting specific commits from another branch can lead to conflicts.

How to Resolve Merge Conflicts: Step-by-Step

1. Identify the Conflict

When a merge conflict occurs, Git provides a clear message indicating which files are affected. For example:

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

The conflicting files are listed, and Git marks the conflict areas in the files.

2. Inspect the Conflict Markers

Open the conflicting file to see the conflict markers added by Git:

<<<<<< HEAD
Your changes in the current branch.
=======
Changes from the branch being merged.
>>>>>> feature-branch
  • <<<<<<< HEAD: Changes from your current branch.
  • =======: Divider between the conflicting changes.
  • >>>>>>>: Changes from the branch being merged.

3. Resolve the Conflict

Decide which changes to keep, or combine the changes manually. For example:

Conflict Example:

<<<<<< HEAD
color: blue;
=======
color: red;
>>>>>> feature-branch

Resolved Conflict:

color: green; /* Combined choice */

Remove the conflict markers (<<<<<<<, =======, >>>>>>>) after resolving.

4. Mark as Resolved

Once all conflicts are resolved, mark the files as resolved using:

$ git add file.txt

You can also use:

$ git mergetool

This opens a merge tool to resolve conflicts interactively.

5. Commit the Merge

After resolving all conflicts and staging the changes, complete the merge by committing:

$ git commit

Git will automatically use a default merge message, or you can write your own.

6. Verify the Merge

Run your application or test suite to ensure everything works as expected after resolving the conflicts.

Strategies to Avoid Merge Conflicts

  1. Pull Changes Frequently: Keep your branch updated with the latest changes from the main branch.
$ git pull origin main
  1. Work on Smaller Changes: Break down large features into smaller, manageable pull requests.
  2. Communicate with Team Members: Coordinate with team members to avoid simultaneous edits on the same files.
  3. Use Feature Flags: For long-running features, use feature flags to avoid integrating incomplete code into the main branch.
  4. Rebase Regularly: Rebase your branch frequently to integrate the latest changes:
$ git rebase main

Advanced Tools for Resolving Merge Conflicts

1. Git Mergetool

Use git mergetool to resolve conflicts with graphical or interactive tools like KDiff3, Meld, or Visual Studio Code.

$ git mergetool

2. VS Code Integration

If you use Visual Studio Code, it highlights conflicts and provides options like "Accept Current," "Accept Incoming," and "Accept Both."

Key Tips for Handling Merge Conflicts

  • Understand the Context: Before resolving a conflict, review the changes to understand their purpose.
  • Document Decisions: If a conflict resolution involves significant changes, document the decision in the commit message.
  • Test Thoroughly: After resolving conflicts, always test the application to ensure functionality isn’t broken.

Conclusion

Merge conflicts in Git are a natural part of collaborative development. By understanding why conflicts happen and following the steps to resolve them, you can minimize downtime and maintain team productivity. With practice, you’ll become adept at handling even the most complex conflicts.

Topics Covered