Merging
Merging
Section titled “Merging”🤔 What is Merging?
Section titled “🤔 What is Merging?”Merging takes changes from one branch and adds them to another. Think of it like two people editing the same document:
- You edit chapters 1-3, your colleague edits chapters 4-6
- You merge your changes into one complete document
# First, switch to the branch you want to merge INTOgit switch main
# Then merge the feature branch INTO maingit merge feature-login⚡ Fast-Forward Merge
Section titled “⚡ Fast-Forward Merge”A fast-forward merge happens when the target branch hasn’t changed since you branched off. Git simply moves the pointer forward.
gitGraph commit id: "Setup" commit id: "Homepage" branch feature-nav checkout feature-nav commit id: "Navigation bar" commit id: "Nav styles" checkout main merge feature-nav id: "Fast-forward" commit id: "Footer"Before merge:
main ──▶ A ──▶ B ▲ feature-nav (pointing to B)After fast-forward merge:
main ──▶ A ──▶ B ▲ main & feature-nav (both at B)git switch maingit merge feature-nav# Updating a1b2c3d..f6e5d4c# Fast-forwardResult: No new merge commit is created. The history stays linear.
🔀 3-Way Merge
Section titled “🔀 3-Way Merge”A 3-way merge happens when the target branch has also moved forward since you branched off.
gitGraph commit id: "Setup" commit id: "Homepage" branch feature-nav checkout feature-nav commit id: "Navigation" checkout main commit id: "Footer" checkout feature-nav commit id: "Nav styles" checkout main merge feature-nav id: "Merge commit" commit id: "Deploy"Before merge:
main: A ──▶ B ──▶ C
feature: A ──▶ B ──▶ D ──▶ EAfter 3-way merge:
main: A ──▶ B ──▶ C ──▶ F (merge commit) \ / D ───── Egit switch maingit merge feature-nav# Merge made by the 'ort' strategy.# (editor opens for a merge commit message)Result: Git creates a merge commit with two parents — one from main, one from the feature branch. History shows both branches.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”# 1. Setupgit initecho "Initial" > file.txtgit add . && git commit -m "Initial commit"
# 2. Create and work on a branchgit checkout -b featureecho "Feature work" >> file.txtgit add . && git commit -m "Feature commit"
# 3. Switch back and make changes on maingit checkout mainecho "Main work" >> file.txtgit add . && git commit -m "Main commit"
# 4. Merge feature into maingit merge feature# (this will create a merge commit)📊 Fast-Forward vs 3-Way
Section titled “📊 Fast-Forward vs 3-Way”| Feature | Fast-Forward | 3-Way Merge |
|---|---|---|
| When it happens | Target branch hasn’t moved | Both branches have new commits |
| Creates a merge commit? | ❌ No | ✅ Yes |
| History | Linear (clean) | Branched (accurate) |
| Default behavior | Git does it automatically | Git prompts for commit message |
In Simple Words
Section titled “In Simple Words”- Merging combines changes from one branch into another
- Fast-forward = simple pointer move (no new commit, clean history)
- 3-way merge = creates a merge commit when both branches moved forward
- Always switch to the target branch first (
git switch main), then merge - After merging, you can safely delete the feature branch