Git Merging & Merge Conflicts - The Complete Guide

Learn how to merge branches and resolve merge conflicts like a pro! Includes step-by-step examples!

Introduction

Let's merge branches and fix merge conflicts!

What You Will Learn

  • Git merge basics
  • Fast-forward merge vs three-way merge
  • How to resolve merge conflicts
  • Merge strategies

Prerequisites

Git Merge

Merging takes changes from one branch and integrates them into another!

Step 1: Switch to the Target Branch!

git switch main

Step 2: Pull Latest Changes!

git pull origin main

Step 3: Merge Your Branch!

git merge feature-login

Types of Merges!

Fast-Forward Merge

No new commits! Just moves the branch pointer forward! Happens when no new changes on main since branching!

graph LR
    C1 -- main --> C2
    C2 -- main/feature-login --> C3
    C3 -- main/feature-login --> C4

Three-Way Merge

Creates a new merge commit! Happens when both branches have new changes!

Merge Conflicts!

Oh no! Git can't resolve differences automatically! Don't panic - they're normal and easy to fix!

How Conflicts Look!

Git marks conflict areas:

<<<<<<< HEAD
This is content from main branch
=======
This is content from feature branch
>>>>>>> feature-login

How to Resolve Conflicts!

  1. Open the file!
  2. Edit the file to keep what you want!
  3. Delete the conflict markers (<<<<<<<, =======, >>>>>>>)!
  4. git add the fixed file!
  5. git commit!

Best Practices!

  1. Merge often: Smaller conflicts!
  2. Communicate: Talk to teammates!
  3. Test after merging: Don't merge broken code!
  4. Don't panic: Conflicts are normal!

Summary

Merge safely and resolve conflicts calmly!

Related Articles

Previous Tutorial

Git Branching

Next Tutorial

Let's learn pull requests! → Pull Requests