How to resolve merge conflicts in a Git repository

If you've been using Git for any time, you've likely encountered this issue. Merge conflicts arise when two branches have changes in the same part of a file and are merged together. Git, being the helpful tool that it is, will not decide for you which changes to accept. Instead, it highlights the problem and asks you to resolve it. With a systematic approach, resolving these conflicts can be straightforward.

Understanding merge conflicts

Before looking into the resolution, it's essential to understand what a merge conflict is. Imagine two developers, Alice and Bob. Alice modifies a function in fileA.txt on her branch, while Bob renames the same function in fileA.txt on his branch. When they try to merge their branches, Git gets confused. Which change should it accept? This is a merge conflict.

Identifying the conflict

When a merge conflict occurs, Git will output a message similar to this:

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

This message indicates that there's a conflict in fileA.txt.

Navigating to the conflict

Open the conflicted file in your preferred text editor. Git will mark the problematic area in the file with the following annotations:

<<<<<<< HEAD
Alice's changes
=======
Bob's changes
>>>>>>> branch-name

The <<<<<<< HEAD marker indicates the start of the changes from the current branch (Alice's changes), while the >>>>>>> branch-name marker represents the end of the changes from the merged branch (Bob's changes). The ======= separates the two sets of changes.

Resolving the conflict

To resolve the conflict, you'll need to decide which changes to keep and which to discard.

  1. Delete the conflict markers (<<<<<<<, =======, >>>>>>>).

  2. Choose which version of the content you want to keep (either Alice's changes, Bob's changes, or a combination of both).

  3. Save the file.

For instance, if you decide to keep Alice's changes:

Alice's changes

Or, if you decide to keep Bob's changes:

Bob's changes

Or, you might combine both:

Alice's changes with some of Bob's modifications

Committing the resolved conflict

Once you've resolved the conflict, stage the changes:

git add fileA.txt

Then, commit the changes:

git commit -m "Resolved merge conflict in fileA.txt"

Tips for avoiding merge conflicts

While merge conflicts are a natural part of collaborative coding, some best practices can help minimize their occurrence:

  • Frequent pulls/pushes: Regularly pull changes from the main branch and push your changes to avoid large, complex conflicts.

  • Clear communication: Coordinate with your team to ensure that multiple people aren't editing the same code sections simultaneously.

  • Use branches wisely: Create specific branches for features or bug fixes to isolate changes.

Conclusion

Merge conflicts might seem daunting initially, but with a systematic approach and understanding of Git's conflict markers, they become manageable. Remember, the key is to communicate with your team and understand the changes made in both conflicting branches. With practice, resolving merge conflicts will become second nature.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved