How to Check and Resolve Conflicts in Git

Git is a powerful tool for version control, but when multiple people (or even the same person) edit the same part of a file in different branches, merge conflicts can occur. Understanding how to check for and resolve these conflicts is essential for smooth collaboration and clean version history.

In this post, you’ll learn how to identify, view, and resolve Git conflicts with practical commands and tips.


⚠️ What Is a Git Conflict?

A Git conflict happens when Git can’t automatically merge changes because two branches have made incompatible edits to the same line or file.

For example:

  • You and a teammate edit the same line of code in different branches.
  • You delete a file in one branch but edit it in another.

Git doesn’t know which change to keep, so it stops and asks you to resolve the conflict.


🔍 How to Check for Git Conflicts

🔹 1. During Merge or Rebase

Conflicts are typically detected during:

git merge branch-name

or

git rebase branch-name

If there are conflicts, Git will respond with something like:

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

🔹 2. Check Conflict Status

Use this command to view a list of files in conflict:

git status

You’ll see output like:

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

        both modified:   file.txt

This clearly shows which files have conflicts.


🧭 How to View and Resolve Conflicts

🔸 1. Open the Conflicted File

Git marks the conflicting sections like this:

<<<<<<< HEAD
Your changes from the current branch
=======
Their changes from the other branch
>>>>>>> branch-name

You’ll need to decide:

  • Keep your version
  • Keep their version
  • Combine both

Edit the file manually to remove the conflict markers and fix the content.


🔸 2. Mark as Resolved

Once you fix the conflicts:

git add file.txt

This tells Git the conflict has been resolved.


🔸 3. Finalize the Merge or Rebase

After resolving all conflicts:

git commit         # If merging
# or
git rebase --continue  # If rebasing

🧠 Bonus: Tools for Conflict Resolution

Use VS Code or a Git GUI

If you use Visual Studio Code, it automatically highlights conflicted sections and offers accept buttons for each side:

  • Accept Current Change
  • Accept Incoming Change
  • Accept Both Changes

Other Git clients with visual merge tools:

  • GitKraken
  • SourceTree
  • GitHub Desktop

✅ Summary of Key Commands

TaskCommand
Merge a branchgit merge branch-name
Check for conflictsgit status
View conflicted fileOpen in editor or GUI tool
Mark file as resolvedgit add <file>
Finalize merge/rebasegit commit or git rebase --continue

🏁 Conclusion

Conflicts in Git are a normal part of collaborative development. Learning how to identify and resolve them quickly ensures that your workflow stays smooth and your codebase remains clean. Always read the conflict markers carefully, test after merging, and communicate with your team if unsure.

Sharing Is Caring:

Leave a Comment