🌿 Phase 1 · Core Skills 🟡 Intermediate MODULE 04

Branches & Merging

⏱️ 40 min
💻 Hands-on
🧩 5 Quiz Questions
🏗️ 1 Challenge
Course progress44%
🎯 What you'll learn: What branches are and why they're Git's superpower. How to create, switch, and merge branches. The difference between fast-forward and 3-way merges. How to resolve merge conflicts. Best practices for branch naming and workflow.

What is a Branch?

A branch is a parallel timeline of your project. The default branch is called main (or master in older repos). When you create a new branch, you get an independent copy of the history where you can make changes without affecting main.

🎬
The parallel universe analogy
Imagine branching as creating a parallel universe. In the main universe, your app is stable and running in production. In a parallel branch universe, you're building a new feature. When it's done and tested, you "merge" the universes — bringing your feature into the main timeline without ever risking the production code.
Branch Timeline Visualization
main    A ─── B ─── C ─────────────── F (merge commit)
                         ╲
feature                    D ─── E
A, B, C = commits on main. D, E = commits on feature branch. F = merge commit bringing feature into main.

Branch Commands

You only need a handful of commands to work with branches effectively. Here are all the essential ones:

All branch commands
BASH
# List all local branches (* = current branch)
git branch

# List all branches including remote
git branch -a

# Create a new branch
git branch feat/user-auth

# Switch to a branch (modern syntax)
git switch feat/user-auth

# Old syntax (still works)
git checkout feat/user-auth

# CREATE and switch in one command
git checkout -b feat/payment-flow
# Or modern:
git switch -c feat/payment-flow

# Delete a merged branch
git branch -d feat/user-auth

# Force delete (even if unmerged)
git branch -D feat/user-auth

# Rename current branch
git branch -m new-name
💡
Branch naming conventions
Use prefixes to categorize branches:
feat/ — new features (feat/user-login)
fix/ — bug fixes (fix/null-pointer)
hotfix/ — urgent production fixes (hotfix/security-patch)
chore/ — maintenance (chore/update-deps)
docs/ — documentation only (docs/api-guide)

Merging Branches

When your feature is complete and tested, you merge it back into main. Git performs the merge automatically in most cases.

Merging a feature branch
BASH
# 1. Switch to the branch you want to merge INTO
git switch main

# 2. Merge the feature branch into main
git merge feat/user-auth

# 3. Delete the feature branch (optional, keeps things clean)
git branch -d feat/user-auth

Fast-forward vs 3-way Merge

Git uses different strategies depending on whether the base branch has diverged from the feature branch:

⚡ Fast-forward Merge
When main has NOT moved since the branch was created. Git simply moves the main pointer forward. No merge commit created. Cleanest history.
main: A ─ B ─ C ─ D ─ E
(branch was at B, main also at B)
🔀 3-way Merge
When main has moved forward since branching. Git creates a merge commit combining both timelines. Shows the true history of parallel work.
main: A ─ B ─ C ─ F
feat:           ╲─ D ─ E ─╯

Merge Conflicts: What They Are & How to Resolve Them

A merge conflict happens when both branches modified the same lines of the same file. Git can't decide which version to keep — it asks you to decide manually.

Don't panic — conflicts are normal
Every developer encounters merge conflicts. They're not errors — they're Git asking for your judgment. Once you've resolved 5 or 10 conflicts, it becomes second nature.
What a conflict looks like + how to resolve it
BASH
# Git puts conflict markers in the file:

<<<<<<< HEAD
const greeting = "Hello";   # Your version (main)
=======
const greeting = "Hi there"; # Their version (feature branch)
>>>>>>> feat/update-greeting

# TO RESOLVE:
# 1. Delete ALL conflict markers (<<<, ===, >>>)
# 2. Keep whichever version you want (or combine them)
# 3. Save the file

# After resolving all conflicts:
git add .
git commit -m "merge: resolve conflict in greeting message"

Using VS Code to Resolve Conflicts

VS Code has built-in merge conflict resolution UI. When you open a conflicted file, it shows buttons: "Accept Current Change", "Accept Incoming Change", "Accept Both Changes". Click the one you want and save. Much easier than manual editing.

git stash — Save Work in Progress

Sometimes you're in the middle of work but need to switch branches urgently (to fix a bug, help a colleague). git stash temporarily shelves your changes without committing.

git stash commands
BASH
# Stash current uncommitted changes
git stash

# Stash with a description
git stash push -m "WIP: half-done login form"

# List all stashes
git stash list

# Apply most recent stash (keeps it in list)
git stash apply

# Apply AND remove from list
git stash pop

# Apply a specific stash
git stash apply stash@{2}

# Delete a stash
git stash drop stash@{0}
🧩 Knowledge Check — Lesson 4
Test your branching knowledge. 5 questions with instant feedback.
1. What command creates a new branch AND immediately switches to it?
2. A merge conflict happens when:
3. In a fast-forward merge, what does Git do?
4. Which branch naming convention is correct for a new feature?
5. What does git stash do?
🌿
Branch Challenge — Lesson 4
Hands-on branching & conflict resolution · Intermediate
Challenge: Create a Feature Branch & Resolve a Merge Conflict

Using the repo from Lesson 3 (or a new one):

1. Create a branch: git checkout -b feat/add-bio
2. Edit README.md — change the first line to "# My Amazing Project"
3. Commit: feat: update project title in README
4. Switch back to main: git switch main
5. Edit the same first line of README.md differently — "# My Awesome Project"
6. Commit on main: docs: rename project in README
7. Merge the feature branch: git merge feat/add-bio
8. Resolve the conflict, stage it, and commit the merge
9. Run git log --oneline --graph to see the branching history
💡 Show hints
  • Conflicts appear as <<<<<<< HEAD ... ======= ... >>>>>>> in the file
  • Delete all conflict markers and keep the text you want
  • After editing: git add README.md then git commit
  • VS Code shows conflict resolution buttons — much easier than manual editing
Finished this lesson?
Mark it complete to track your progress.
🎉

Lesson 4 Complete!

Branches mastered! Now let's take your code to the cloud with GitHub.

Lesson 04 of 09Git & GitHub Mastery