Merging Basics

Loading concept...

🌿 Git Merging Basics: Bringing Your Work Together

The River Story

Imagine a big river. One day, the river splits into two smaller streams. Each stream flows its own path — one goes through a forest, the other through a meadow.

But eventually, both streams come back together and become one big river again!

That’s exactly what merging is in Git!

When you create a branch, you’re splitting off from the main path. When you merge, you’re bringing everything back together.


🤝 What is Merging Branches?

Merging is like putting puzzle pieces together. You worked on something in your branch, and now you want to add it to the main project.

Simple Example:

You and your friend are writing a story together:

  • Main branch: Has the beginning of the story
  • Your branch: You added a cool adventure in the middle
  • Merge: Now the main story includes your adventure!
graph TD A[Main Story] --> B[Your Branch] A --> C[Main continues...] B --> D[Your Adventure] D --> E[Merge!] C --> E E --> F[Complete Story]

The Basic Command:

git checkout main
git merge your-branch-name

First, go to where you want to add changes. Then, bring in the other branch!


⚡ Fast-Forward Merge

The Straight Road

Imagine you’re walking on a road. You step off to pick some flowers. When you come back, no one else walked ahead — the road is exactly where you left it!

Fast-forward merge happens when:

  • Main branch didn’t change while you worked
  • Git just moves the pointer forward

Picture This:

graph TD A[Main - Commit 1] --> B[Commit 2] B --> C[Commit 3] C --> D["Feature Branch<br>#40;Commit 4#41;"] D --> E[Commit 5] E --> F["Main moves here!<br>#40;Fast-forward#41;"]

What Happens:

git checkout main
git merge feature

Before merge:

  • Main is at Commit 3
  • Feature has Commits 4 and 5

After merge:

  • Main jumps to Commit 5
  • No new commit created!

Why “Fast-Forward”?

It’s like fast-forwarding a video. Git doesn’t need to do any special work — it just moves ahead to catch up!

Real Life Example:

  • You wrote Chapter 2 of a book
  • No one touched Chapter 1 while you worked
  • Git just adds Chapter 2 after Chapter 1
  • Done! Simple!

🔀 Three-Way Merge

The Meeting Point

Now imagine TWO streams flowing from the same river. Both streams have new water in them. When they meet again, the river needs water from BOTH streams!

Three-way merge happens when:

  • Main branch changed while you worked
  • Your branch also has changes
  • Git combines BOTH!

The “Three Ways” Are:

  1. Common Ancestor — Where both branches started
  2. Your Branch — What you added
  3. Main Branch — What others added
graph TD A["Common Ancestor<br>#40;Where we started#41;"] --> B[Main's changes] A --> C[Your changes] B --> D["Merge Commit<br>#40;Combines both!#41;"] C --> D

Example:

# On main branch
git merge feature-branch

Git thinks:

  • “What did Main add? OK, got it!”
  • “What did Feature add? OK, got it!”
  • “Let me put both together nicely!”

Why Three-Way?

Git looks at THREE snapshots:

  1. The starting point (ancestor)
  2. Your ending point
  3. Main’s ending point

Then it figures out what’s new in each!


📦 Merge Commits

The Special Box

When Git does a three-way merge, it creates a merge commit. Think of it as a special box that holds handles to BOTH parent branches!

What Makes It Special:

Regular commits have ONE parent:

Commit 5 → came from → Commit 4

Merge commits have TWO parents:

Merge Commit → came from → Commit 5 (main)
             → came from → Commit 7 (feature)

Visual:

graph TD A[Commit 1] --> B[Commit 2] B --> C[Commit 3 - Main] B --> D[Commit 4 - Feature] D --> E[Commit 5 - Feature] C --> F["Merge Commit<br>#40;Has 2 parents!#41;"] E --> F F --> G[Continue working...]

The Merge Message:

git merge feature-branch

Git automatically creates a message:

Merge branch 'feature-branch' into main

You can also write your own:

git merge feature-branch -m "Add login feature"

Why Merge Commits Matter:

  • They tell the STORY of your project
  • You can see when branches joined
  • Easy to undo if needed!

🎯 Squash Merge

The Cleanup Crew

Imagine you’re baking a cake. You:

  1. Got the eggs
  2. Mixed the flour
  3. Added sugar
  4. Spilled some, cleaned up
  5. Finally finished!

But when you tell the story, you just say: “I baked a cake!”

That’s squash merge! It takes ALL your little commits and squishes them into ONE clean commit!

Without Squash:

Feature Branch:
- "started work"
- "fix typo"
- "oops wrong file"
- "actually done now"
- "forgot one thing"

With Squash:

Main Branch:
- "Add awesome new feature" ✨

The Command:

git checkout main
git merge --squash feature-branch
git commit -m "Add new feature"

Visual:

graph TD A[Main] --> B[Feature Start] B --> C[Fix 1] C --> D[Fix 2] D --> E[Fix 3] E --> F["SQUASH!"] F --> G["One clean commit<br>on Main"] A --> G

When to Use Squash:

  • âś… Many small “work-in-progress” commits
  • âś… Want a clean history
  • âś… Details don’t matter, just the result

When NOT to Use Squash:

  • ❌ Each commit tells important story
  • ❌ You might need to undo specific changes
  • ❌ Other people need to see your process

🎓 Quick Comparison

Merge Type When It Happens Creates New Commit? History
Fast-Forward Main didn’t change No Linear, clean
Three-Way Both branches changed Yes (merge commit) Shows branches
Squash You choose it Yes (single commit) Super clean

🌟 Remember!

Merging = Bringing Work Together

  • Fast-forward = Simple catch-up (just move forward!)
  • Three-way = Combine two different paths
  • Merge commit = Special commit with two parents
  • Squash = Combine many commits into one

The Golden Rule:

Always make sure you’re ON the branch you want to ADD TO, then merge the other branch INTO it!

# I want to add feature to main
git checkout main      # Go to main first!
git merge feature      # Bring feature into main

You’re now a merging master! 🎉


đź§Ş Try It Yourself!

  1. Create a new branch
  2. Make some commits
  3. Go back to main
  4. Try merging!

Watch what Git tells you — it will say “fast-forward” or “merge made”!

Happy merging! 🌿

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.