Chapter 13: Git Branch Merge

Git Branch Merge — the moment when you say “this experiment/feature/bugfix is good — let’s bring it into the main story”.

git merge is Git’s way of joining two (or more) lines of development back together into one unified history. It takes all the commits from one branch and integrates them into another branch (usually main).

Think of it like this:

  • You have the official book (main branch)
  • Your friend wrote a new chapter in a side copy (feature/login branch)
  • Merge = carefully copy-paste the new chapter into the official book → If no overlap/conflict → automatic & clean → If same paragraph edited differently → Git pauses and asks you to decide who wins each line

There are two main flavors of merge in everyday use (2026 reality):

  1. Fast-forward merge (clean & linear — most beautiful)
  2. True merge commit (creates a diamond/merge bubble — preserves full story)

We’ll do both with a real example.

Step-by-Step Hands-on Example: Merging a Feature Branch

Start fresh:

Bash

Commit 1–2 on main (base story)

Bash

Now create & work on a feature branch:

Bash

Make changes on the feature branch:

Bash

Add one more commit for realism:

Bash

Check graph:

Bash

Something like:

text

Now — time to merge!

Scenario 1: Fast-forward merge (clean & linear – no extra commit)

Switch back to target branch:

Bash

Merge the feature in:

Bash

Output you’ll likely see:

text

Now graph:

Bash
text

→ Perfect straight line! Git just moved main pointer forward to catch up — no new commit needed. This is the cleanest history (most teams love it).

Scenario 2: True merge commit (when branches diverged)

Simulate divergence — someone else (or you) continued on main:

Bash

Now graph before merge:

text

Merge again:

Bash

Git says:

text

And creates a merge commit automatically (unless conflicts).

Graph now:

text

→ Diamond shape! The merge commit has two parents (one from main, one from feature). History tells the true story: “these lines happened in parallel”.

Merge Conflicts – When Git Needs Your Help

If both branches changed the same line differently:

Example conflict:

  • main: changed line 3 to “Buy vegetables”
  • feature: changed line 3 to “Buy milk & eggs”

Git stops:

text

git status shows:

text

Open README.md — Git marks:

text

You decide:

  • Keep one version
  • Combine both
  • Rewrite

Then:

Bash

Done!

Abort if overwhelmed:

Bash

Common Merge Options / Flags (Useful in 2026)

Flag / Option Command Example When / Why use it
Fast-forward only git merge –ff-only feature Fail if cannot fast-forward (clean history)
No fast-forward git merge –no-ff feature Force merge commit even if fast-forward possible
Squash merge git merge –squash feature Combine all commits into one (then commit yourself)
No commit (inspect first) git merge –no-commit feature Merge files but don’t auto-commit
Strategy (rare) git merge -s ort feature Default now — ort is fast & smart
Ignore whitespace git merge -Xignore-all-space feature Conflicts only on real code, not formatting

Summary Table – Merge vs Alternatives

Action Command History Style Best For
Bring feature to main git merge feature Merge commit or FF Integrating finished work
Re-write history linear git rebase main (on feature) Linear (no merge bubble) Clean personal/feature branches
Pull + merge git pull (default = fetch + merge) Same as merge Update main from remote
Pull + rebase git pull –rebase Linear Keep history clean when pulling

Quick workflow most people do in 2026:

Bash

Got the merge feeling? Merge = “integrate this branch’s story into mine”.

Next class?

  • Real merge conflict resolution step-by-step?
  • Squash merge vs regular?
  • Rebase vs merge deep dive?
  • GitHub Pull Request merge options?

Just say — we’ll keep going. You’re mastering branches & merges now! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *