How to Fix Conflicts in Git: A Practical Guide

When working on collaborative projects using Git, you may occasionally run into merge conflicts—situations where Git can’t automatically reconcile differences between branches. These conflicts can seem intimidating at first, but with the right approach, they’re easy to resolve.

In this guide, we’ll walk you through how to identify, resolve, and commit conflict fixes in Git.


❓ What Is a Git Merge Conflict?

A merge conflict occurs when:

  • Two branches have modified the same part of a file, and Git cannot determine which version to keep.
  • A file has been deleted in one branch but modified in another.

These situations require manual intervention to tell Git how to proceed.


🔍 How to Identify a Merge Conflict

When you try to merge, rebase, or pull changes, you might see an error like:

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

To view the conflicted files:

git status

It will list conflicted files under:

Unmerged paths:
  (use "git add <file>..." to mark resolution)

✅ Steps to Fix a Merge Conflict

🔹 Step 1: Open the Conflicted File

Git will mark the conflict inside the file like this:

<<<<<<< HEAD
Your changes (current branch)
=======
Incoming changes (branch being merged)
>>>>>>> branch-name

You need to manually edit the file and decide which version to keep—or combine them.

🔹 Step 2: Edit and Resolve the Conflict

Decide between:

  • Keeping your changes
  • Keeping the incoming changes
  • Merging both logically

Then, remove the conflict markers (<<<<<<<, =======, >>>>>>>) from the file.

🔹 Step 3: Stage the Resolved File

Once you’ve resolved the conflict, stage the file:

git add filename

🔹 Step 4: Complete the Merge or Rebase

If you were merging:

git commit -m "Resolved merge conflict in filename"

If you were rebasing:

git rebase --continue

🛠 Tools That Help Resolve Conflicts

  • VS Code: Offers a graphical merge editor.
  • GitKraken / Sourcetree: Visual tools to manage conflicts.
  • git mergetool: Launches an external tool to assist in resolving conflicts.

To launch the default mergetool:

git mergetool

You can configure which tool to use via:

git config --global merge.tool meld  # or your tool of choice

🚫 How to Abort a Merge (If Needed)

If you’re stuck or want to start over:

git merge --abort

For rebases:

git rebase --abort

✅ Summary

TaskCommand
Check for conflictsgit status
Manually resolve in fileEdit and remove conflict markers
Stage resolved filesgit add filename
Complete the mergegit commit
Abort a merge (optional)git merge --abort
Use merge tool (optional)git mergetool

🚀 Final Thoughts

Merge conflicts are a normal part of Git-based workflows. The key is to stay calm, understand the differences, and resolve them logically. With practice, you’ll handle conflicts smoothly—and even learn how to prevent them with smart branching strategies.

Sharing Is Caring:

Leave a Comment