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!
- Open the file!
- Edit the file to keep what you want!
- Delete the conflict markers (
<<<<<<<,=======,>>>>>>>)! git addthe fixed file!git commit!
Best Practices!
- Merge often: Smaller conflicts!
- Communicate: Talk to teammates!
- Test after merging: Don't merge broken code!
- Don't panic: Conflicts are normal!
Summary
Merge safely and resolve conflicts calmly!
Related Articles
Previous Tutorial
Next Tutorial
Let's learn pull requests! → Pull Requests