Falytom logoFALYTOM
Games
ToolsUI ComponentsServicesProductsAbout
menu
  1. Home
  2. /Web Development
  3. /Github
  4. /Handling Merge Conflicts
Your Progress0%
0 of 20 completed

Git & Github Topics

Getting Started

  • What is Git?
  • What is GitHub?
  • Installing & Configuring Git
  • Your First Git Repository

Core Git Concepts

  • Understanding Commits & History
  • Working with Branches
  • Merging Branches
  • The Staging Area
  • Undoing Changes

Working with GitHub

  • Connecting to GitHub
  • Pushing & Pulling Code
  • Cloning Repositories
  • GitHub README & Documentation

Collaboration Workflows

  • Forking & Pull Requests
  • Code Review Basics
  • Handling Merge Conflicts
  • Issues & Project Management

Best Practices & Workflows

  • Git Workflow Best Practices
  • Working with .gitignore
  • Common Git Problems & Solutions

Handling Merge Conflicts

Understand and resolve Git merge conflicts confidently

Merge conflicts are one of the most intimidating parts of Git for beginners, but they're actually straightforward once you understand them. A conflict simply means Git found changes it can't automatically combine and needs your help deciding what to keep. In this lesson, you'll learn what causes conflicts, how to read conflict markers, resolve conflicts step-by-step, use merge tools, and prevent conflicts from happening in the first place. By the end, conflicts won't be scaryβ€”they'll just be part of your normal workflow!

What Are Merge Conflicts?

A merge conflict occurs when Git can't automatically combine changes from different branches.

When Do Conflicts Happen?

Conflicts occur when:

  • Same lines changed: Two branches modify the same lines in a file
  • File deleted in one branch: One branch deletes a file another branch modifies
  • File renamed: Different branches rename the same file differently

The good news:

Most merges don't have conflicts! Git automatically merges changes to different parts of files. Conflicts only happen when changes overlap in ways Git can't reconcile automatically.

Example Scenario

TEXT
Starting point (main branch):
<h1>Welcome</h1>

You (on main):
<h1>Welcome to My Site</h1>

Teammate (on feature branch):
<h1>Welcome to Our Website</h1>

Both changed the same line!
Git can't decide which to keep β†’ CONFLICT

πŸ’‘ Conflicts Are Normal

Merge conflicts are a normal part of collaborative development. They're not errors or bugsβ€”they're Git asking for your help making a decision. Don't panic when you see them!

Anatomy of a Conflict

When a conflict occurs, Git adds special markers to the file to show you what's conflicting:

HTML
<<<<<<< HEAD
<h1>Welcome to My Site</h1>
=======
<h1>Welcome to Our Website</h1>
>>>>>>> feature-branch

Understanding the Markers

Conflict markers explained:

  • <<<<<<< HEAD
    • Marks the start of the conflict
    • HEAD = your current branch (what you have)
  • =======
    • Separates the two conflicting versions
  • >>>>>>> feature-branch
    • Marks the end of the conflict
    • Shows what branch you're merging from

Visual Breakdown

TEXT
<<<<<<< HEAD              ← Your changes start here
<h1>Welcome to My Site</h1>   ← What YOU have
=======                   ← Separator
<h1>Welcome to Our Website</h1> ← What's INCOMING
>>>>>>> feature-branch    ← Incoming changes end here

Multiple Conflicts in One File

A file can have multiple conflict sections:

JAVASCRIPT
function greet() {
<<<<<<< HEAD
  console.log("Hello!");
=======
  console.log("Hi there!");
>>>>>>> feature-branch
}

function calculate() {
<<<<<<< HEAD
  return x + y;
=======
  return x * y;
>>>>>>> feature-branch
}

You'll need to resolve each conflict section independently.

Detecting Conflicts

When Merging

You'll see a conflict message when merging:

BASH
git merge feature-branch

# Output:
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Check Status

BASH
git status

# Output:
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   index.html
        both modified:   app.js

no changes added to commit

Identify Conflicted Files

Files with conflicts are marked as "both modified" in git status. These are the files you need to fix.

Resolving Conflicts Step-by-Step

Step 1: Identify Conflicted Files

BASH
git status

# Look for "both modified" files

Step 2: Open Conflicted File

Open the file in your editor. You'll see conflict markers:

index.html
<!DOCTYPE html>
<html>
<head>
  <title>My Website</title>
</head>
<body>
<<<<<<< HEAD
  <h1>Welcome to My Site</h1>
  <p>This is my personal website.</p>
=======
  <h1>Welcome to Our Website</h1>
  <p>This is our company website.</p>
>>>>>>> feature-branch
</body>
</html>

Step 3: Decide What to Keep

You have several options:

Option A: Keep Your Version

HTML
<body>
  <h1>Welcome to My Site</h1>
  <p>This is my personal website.</p>
</body>

Option B: Keep Their Version

HTML
<body>
  <h1>Welcome to Our Website</h1>
  <p>This is our company website.</p>
</body>

Option C: Keep Both (Combined)

HTML
<body>
  <h1>Welcome to Our Website</h1>
  <p>This is our company's personal website.</p>
</body>

Option D: Write Something New

HTML
<body>
  <h1>Welcome</h1>
  <p>Explore our site to learn more.</p>
</body>

Critical: You MUST remove ALL conflict markers (<<<<<<<, =======, >>>>>>>) from the file!

Step 4: Mark as Resolved

After editing, stage the file:

BASH
# Stage the resolved file
git add index.html

# Check status
git status
# Shows: "All conflicts fixed but you are still merging."

Step 5: Complete the Merge

BASH
# Commit the merge
git commit

# Git opens editor with pre-filled merge message:
# "Merge branch 'feature-branch'"
# You can edit it or just save and close

Done! The conflict is resolved and the merge is complete.

Complete Example

BASH
# Start merge
git merge feature-branch
# CONFLICT (content): Merge conflict in index.html

# Check what's conflicted
git status

# Open and edit index.html to resolve conflicts
# Remove conflict markers, keep desired changes

# Stage resolved file
git add index.html

# Check everything is resolved
git status

# Complete merge
git commit -m "Merge feature-branch, resolved conflicts in index.html"

# Verify
git log --oneline -3

Conflict Resolution Strategies

Strategy 1: Accept One Side Completely

Use when one version is clearly correct:

BASH
# Keep your version (ours)
git checkout --ours index.html
git add index.html

# Keep their version (theirs)
git checkout --theirs index.html
git add index.html

Use with caution! This accepts ALL changes in the file from one side. Only use when you're certain one side is completely correct.

Strategy 2: Manual Resolution

Most common approachβ€”manually edit to combine best of both:

JAVASCRIPT
// Before (conflict):
<<<<<<< HEAD
function calculate(a, b) {
  return a + b;
}
=======
function calculate(x, y) {
  return x * y;
}
>>>>>>> feature-branch

// After (resolved):
function calculate(a, b) {
  // Combined: kept parameter names from HEAD, operation from feature
  return a * b;
}

Strategy 3: Rewrite Both Sections

When neither version is quite right:

JAVASCRIPT
// Before (conflict):
<<<<<<< HEAD
if (user) return true;
=======
if (user && user.active) return true;
>>>>>>> feature-branch

// After (better solution):
if (user?.active) return true;  // Modern optional chaining

Using Merge Tools

Visual merge tools make conflict resolution easier by showing three-way diffs:

Built-in Git Mergetool

BASH
# Launch configured merge tool
git mergetool

# Git will open each conflicted file in the tool

Popular Merge Tools

VS Code (Built-in)

VS Code automatically detects conflicts and shows buttons:

  • Accept Current Change: Keep your version
  • Accept Incoming Change: Keep their version
  • Accept Both Changes: Keep both
  • Compare Changes: See side-by-side diff

Configure Git to Use VS Code

BASH
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'

Other Popular Tools

  • KDiff3: Free, cross-platform
  • P4Merge: Free from Perforce
  • Meld: Open-source, Linux/Windows/Mac
  • Beyond Compare: Commercial, powerful
  • Sublime Merge: From Sublime Text makers

Three-Way Merge View

Most merge tools show three panels:

TEXT
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    YOURS    β”‚    BASE     β”‚   THEIRS    β”‚
β”‚  (current)  β”‚  (common)   β”‚ (incoming)  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            RESULT (merged)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • Yours: Your current branch changes
  • Base: Common ancestor (what both started from)
  • Theirs: Incoming branch changes
  • Result: What you're creating (resolved version)

Aborting a Merge

If conflicts are too complex or you made a mistake, you can abort:

BASH
# Abort the merge
git merge --abort

# Your repository returns to state before merge started
# No changes lost!

When to abort:

  • Conflicts are more complex than expected
  • You realize you merged the wrong branch
  • You need to discuss approach with team first
  • You want to try a different merge strategy

After aborting, you can:

  • Plan a better approach
  • Discuss with team
  • Work on smaller, focused merges
  • Use rebase instead of merge

Practice Resolving Conflicts

Use this interactive conflict resolver to practice:

Merge Conflict Resolver

Practice resolving merge conflicts

Merge conflict in app.js

Automatic merge failed. Fix conflicts and commit the result.

Conflict Markers

<<<<<<< main
function greeting() {
  console.log("Hello, World!");
  return "Welcome!";
}
=======
function greeting() {
  console.log("Hello, Developer!");
  return "Welcome to our app!";
}
>>>>>>> feature/new-ui

Current (main)

function greeting() {
  console.log("Hello, World!");
  return "Welcome!";
}

Incoming (feature/new-ui)

function greeting() {
  console.log("Hello, Developer!");
  return "Welcome to our app!";
}

Your Resolution

Common Conflict Scenarios

Scenario 1: Simple Text Conflict

TEXT
# Both branches changed a title
<<<<<<< HEAD
# User Guide
=======
# Getting Started Guide
>>>>>>> docs-update

# Resolution - combine both ideas:
# Getting Started - User Guide

Scenario 2: Code Logic Conflict

JAVASCRIPT
# Both branches modified validation
<<<<<<< HEAD
if (age >= 18) {
  return true;
}
=======
if (age >= 21) {
  return true;
}
>>>>>>> feature-age-check

# Resolution - add parameter to make it flexible:
function canAccess(age, minimumAge = 18) {
  return age >= minimumAge;
}

Scenario 3: Import Statement Conflicts

JAVASCRIPT
# Both added different imports
<<<<<<< HEAD
import React from 'react';
import { useState } from 'react';
=======
import React from 'react';
import { useEffect } from 'react';
>>>>>>> feature-effects

# Resolution - keep both:
import React from 'react';
import { useState, useEffect } from 'react';

Scenario 4: Delete vs Modify Conflict

BASH
# One branch deleted file, other modified it
# Git shows:
CONFLICT (modify/delete): old-config.js deleted in feature-branch 
and modified in HEAD.

# Resolution options:
# 1. Keep deletion (their side was right to remove it)
git rm old-config.js

# 2. Keep file (your changes were important)
git add old-config.js

Preventing Merge Conflicts

While conflicts are normal, you can reduce their frequency:

1. Communicate with Team

  • Discuss who's working on what
  • Coordinate changes to shared files
  • Use separate files when possible

2. Commit and Sync Frequently

BASH
# Pull often to get latest changes
git pull origin main

# Commit small, focused changes
git commit -m "Add user validation"

# Push regularly
git push origin feature-branch

3. Keep Branches Short-Lived

  • Don't let feature branches drift too long
  • Merge or rebase with main regularly
  • Smaller PRs = fewer conflicts

4. Use Feature Flags

Instead of long-running branches, use feature flags:

JAVASCRIPT
// Merge incomplete features behind flags
if (featureFlags.newDashboard) {
  return <NewDashboard />;
}
return <OldDashboard />;

5. Modularize Code

Well-structured code has fewer conflicts:

  • Small, focused files
  • Clear module boundaries
  • Avoid giant monolithic files

6. Agree on Formatting

Use automated formatters to prevent style conflicts:

BASH
# Use Prettier, Black, gofmt, etc.
# Configure in project:
npm install --save-dev prettier
# Add .prettierrc configuration

# Format before committing
npx prettier --write .

7. Rebase Before Opening PR

BASH
# Update your branch with main before PR
git checkout main
git pull
git checkout feature-branch
git rebase main
# Resolve any conflicts now
git push --force-with-lease

🎯 Pull Often, Push Often

The single best way to minimize conflicts: stay synchronized with your team. Pull before you start work, and push when you finish. The longer branches diverge, the more conflicts you'll have!

Practice Conflict Commands

Try these commands to practice identifying and working with conflicts:

Practice Conflict Resolution

Commands for handling merge conflicts

$

Try these examples:

Advanced Conflict Techniques

Rerere (Reuse Recorded Resolution)

Git can remember how you resolved conflicts:

BASH
# Enable rerere
git config --global rerere.enabled true

# Now Git remembers your conflict resolutions
# If same conflict appears again, it auto-resolves!

Checking Conflict History

BASH
# See which commits caused conflict
git log --merge

# See what both sides changed
git log --left-right HEAD...MERGE_HEAD

# Detailed diff
git diff HEAD...MERGE_HEAD

Merge Strategies

BASH
# Different merge strategies for different scenarios

# Recursive (default) - most common
git merge -s recursive feature-branch

# Ours - keep our version in conflicts
git merge -s ours feature-branch

# Theirs - prefer their changes (via recursive option)
git merge -X theirs feature-branch

# Ours option - keep our changes when conflicts
git merge -X ours feature-branch

Command Reference

Here's a quick reference for handling conflicts:

BASH
# Detecting Conflicts
git status                        # See conflicted files
git diff                          # View conflict details
git log --merge                   # See conflicting commits

# Resolving Conflicts
# 1. Edit files manually to resolve
# 2. Remove conflict markers
git add <file>                    # Mark as resolved
git commit                        # Complete merge

# Using One Side
git checkout --ours <file>        # Keep your version
git checkout --theirs <file>      # Keep their version
git add <file>

# Merge Tools
git mergetool                     # Launch merge tool
git mergetool --tool=vimdiff      # Use specific tool

# Aborting
git merge --abort                 # Cancel the merge
git reset --hard HEAD             # Discard all changes (careful!)

# Advanced
git rerere                        # Manually trigger rerere
git config rerere.enabled true    # Enable automatic rerere
git merge -X ours <branch>        # Prefer our changes
git merge -X theirs <branch>      # Prefer their changes

Key Takeaways

  • Conflicts occur when the same lines are changed in different branches
  • Conflict markers show: <<<<<<< (yours), ======= (separator), >>>>>>> (theirs)
  • To resolve: edit file, remove markers, add file, commit
  • Use git status to see which files have conflicts
  • git merge --abort safely cancels a merge
  • Visual merge tools make resolution easier with three-way diffs
  • You can accept one side completely with --ours or --theirs
  • Prevent conflicts by communicating, syncing often, and keeping branches short
  • Conflicts are normalβ€”don't panic, just work through them systematically
  • Enable rerere to automatically reuse conflict resolutions

What's Next?

Excellent work! You now understand merge conflicts and can resolve them confidently. Conflicts are no longer scaryβ€”they're just another part of collaborative development!

In the next lesson, we'll explore Issues & Project Management. You'll learn how to use GitHub Issues to track bugs and features, organize work with labels and milestones, manage projects effectively, and coordinate team efforts. Issues are how successful teams plan and track their work on GitHub!

🎯 Practice Assignment

Before the next lesson, practice resolving conflicts:

  1. Create a test repository with two branches
  2. Make conflicting changes to the same file in both branches
  3. Merge one branch into the other
  4. Resolve the conflicts manually
  5. Try using a merge tool (like VS Code's built-in tool)
  6. Practice using git checkout --ours and --theirs
  7. Try git merge --abort and start over

The more you practice, the more comfortable you'll become with conflicts!

Test Your Understanding of Merge Conflicts

Question 1 of 4

When do merge conflicts occur?

Current Score0 / 0

Know someone who'd find this guide helpful? Please share!

Previous
Code Review Basics
Next
Issues & Project Management

Never Miss a New Git & GitHub Tutorial

Join 2,000+ developers learning Git & GitHub step-by-step. Get new tutorials, tips, and exclusive resources delivered to your inbox - completely FREE.

You might also like

UI Components

Ready-made components for your projects

Developer Tools

Boost productivity with handy generators

HTML Game

Learn HTML concepts through interactive gameplay

Join Our Web Development Training Program

Master frontend & backend development with hands-on projects.

Git & Github Tutorials

0 of 20 completed

Your Progress0%

Topics

Getting Started

  • What is Git?
  • What is GitHub?
  • Installing & Configuring Git
  • Your First Git Repository

Core Git Concepts

  • Understanding Commits & History
  • Working with Branches
  • Merging Branches
  • The Staging Area
  • Undoing Changes

Working with GitHub

  • Connecting to GitHub
  • Pushing & Pulling Code
  • Cloning Repositories
  • GitHub README & Documentation

Collaboration Workflows

  • Forking & Pull Requests
  • Code Review Basics
  • Handling Merge Conflicts
  • Issues & Project Management

Best Practices & Workflows

  • Git Workflow Best Practices
  • Working with .gitignore
  • Common Git Problems & Solutions
Falytom logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and more through interactive games, tutorials, components and free tools. Transform your businesses with AI chatbots, SEO-optimized websites, and professional development services.

Β© 2025 Tomilola Group
  • Facebook logo
  • Twitter logo
  • Instagram logo
  • LinkedIn logo
  • GitHub logo
  • YouTube logo