Resolving Merge Conflicts in Git

Difficulty: intermediate
Est. Time: 40 minutes
Prerequisites:
  • Branching and Merging in Git
Resolving Merge Conflicts in Git
12 min
TUTORIAL
git
merge-conflicts
intermediate

Resolving Merge Conflicts in Git

Merge conflicts occur when Git cannot automatically merge changes from two branches that modify the same part of a file differently. In this blog, we’ll learn how to identify and resolve conflicts effectively.

Table of Contents

  • What Causes Merge Conflicts?
  • Identifying Merge Conflicts
  • Resolving Conflicts Manually
  • Using Merge Tools
  • Exercise: Resolving a Conflict

What Causes Merge Conflicts?

Merge conflicts happen when:

  • Two branches modify the same part of a file differently.
  • A branch deletes a file while another modifies it.

Identifying Merge Conflicts

When a conflict occurs, Git marks the conflicting areas in the file like this:


  <<<<<<< HEAD
  Changes from main
  =======
  Changes from feature-branch
  >>>>>>> feature-branch
          

Resolving Conflicts Manually

  1. Open the conflicting file and locate the conflict markers.
  2. Edit the file to decide which changes to keep.
  3. Stage the resolved file:
  4. 
      git add conflicting-file
                
  5. Commit the resolution:
  6. 
      git commit -m "Resolved merge conflict"
                

Using Merge Tools

Graphical merge tools like VS Code, IntelliJ, or git mergetool can simplify conflict resolution:


  git mergetool
          

Exercise

Simulate a merge conflict by modifying the same file in two branches. Resolve the conflict manually or using a merge tool. Run git log --graph to confirm the merge was successful.

Coming Up Next

In the next part of this series, we’ll explore advanced Git commands like cherry-picking and interactive rebase for fine-grained control over your commit history.

Part 4 of 24 in Git Mastery Series: From Beginner to Expert
All Posts in This Series