Courses/Git/Resolving Merge Conflicts

    Lesson 5 • Intermediate

    Resolving Merge Conflicts 🌿

    Merge conflicts aren't scary once you understand them. Learn why they happen, how to read conflict markers, and how to resolve them confidently.

    What You'll Learn in This Lesson

    • • Why and when merge conflicts occur
    • • Reading conflict markers (<<<<<<<, =======, >>>>>>>)
    • • Step-by-step conflict resolution
    • • Using VS Code to resolve conflicts visually
    • • Preventing conflicts with smart workflows

    1️⃣ Why Conflicts Happen

    Git is incredibly smart at merging changes automatically. Conflicts only happen when two branches modify the exact same lines in the same file. Different files or different lines? Git handles it automatically.

    Try It: Why Conflicts?

    Understand what does and doesn't cause conflicts

    Try it Yourself »
    JavaScript
    // Why Merge Conflicts Happen
    console.log("=== What Causes a Conflict? ===");
    console.log("A conflict occurs when two branches modify the SAME");
    console.log("lines in the SAME file, and Git can't decide which to keep.");
    console.log();
    
    console.log("=== Scenario ===");
    console.log("Original file (main, line 5):");
    console.log('  color: blue;');
    console.log();
    
    console.log("Alice's branch changes line 5 to:");
    console.log('  color: red;');
    console.log();
    
    console.log("Bob's branch changes line 5
    ...

    2️⃣ Reading Conflict Markers

    When a conflict occurs, Git marks the conflicted sections with special markers. Everything between <<<<<<< and ======= is your version; between ======= and >>>>>>> is theirs.

    Try It: Conflict Markers

    Read and understand conflict marker syntax

    Try it Yourself »
    JavaScript
    // Reading Conflict Markers
    console.log("=== Conflict Markers Explained ===");
    console.log("Git marks conflicts in the file with special markers:");
    console.log();
    
    console.log("  body {");
    console.log("    font-size: 16px;");
    console.log("  <<<<<<< HEAD");
    console.log("    color: red;          ← YOUR changes (current branch)");
    console.log("  =======");
    console.log("    color: green;        ← THEIR changes (incoming branch)");
    console.log("  >>>>>>> feature-branch");
    console.log("    margin: 0;
    ...

    3️⃣ Resolving Step by Step

    Resolving is simple: open the file, choose which changes to keep (or combine them), remove the markers, stage the file, and commit. You can always abort with git merge --abort.

    Try It: Resolve a Conflict

    Walk through a complete conflict resolution

    Try it Yourself »
    JavaScript
    // Step-by-Step: Resolving a Conflict
    console.log("=== Conflict Resolution Workflow ===");
    console.log();
    
    const steps = [
      { step: "Merge triggers conflict", cmd: "git merge feature-auth", out: "CONFLICT in src/App.js" },
      { step: "Check which files conflict", cmd: "git status", out: "both modified: src/App.js" },
      { step: "Open conflicted file", cmd: "code src/App.js", out: "(see conflict markers)" },
      { step: "Edit: choose/combine changes", cmd: "(manually edit the file)", out: "Remove al
    ...

    4️⃣ Preventing Conflicts

    The best conflict is one that never happens. Pull frequently, keep branches small, communicate with your team, and use .gitignore to exclude generated files.

    Try It: Prevention

    Strategies and workflows to minimize conflicts

    Try it Yourself »
    JavaScript
    // Preventing & Minimizing Conflicts
    console.log("=== Prevention Strategies ===");
    console.log();
    
    const strategies = [
      { strategy: "Pull frequently", why: "Stay up-to-date with main to reduce divergence" },
      { strategy: "Small, focused branches", why: "Less code changed = fewer potential conflicts" },
      { strategy: "Communicate with team", why: "Know who's working on what files" },
      { strategy: "Merge main into your branch regularly", why: "Catch conflicts early while they're small" },
      { 
    ...

    ⚠️ Common Mistakes

    ⚠️
    Leaving conflict markers in code — Always search for <<<<<<< before committing. These break your code if left in!
    ⚠️
    Always keeping "mine" — Don't blindly accept your changes. Read both versions carefully and choose the correct one.
    💡
    Pro Tip: Use git diff --check before committing — it warns you if conflict markers are still present in any file.

    📋 Quick Reference

    CommandPurpose
    git statusSee conflicted files
    git add fileMark conflict as resolved
    git commitComplete the merge
    git merge --abortCancel merge entirely
    git diff --checkCheck for leftover markers

    🎉 Lesson Complete!

    Merge conflicts no longer scare you! Next, we'll explore professional Git workflows and commit conventions used by real teams.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service