2. Git Core Concepts

Git Branching & Merging Explained Simply

Keep your main branch always deployable: create isolated feature branches with git switch -c, switch timelines safely, and merge finished features cleanly.

3 min readUpdated 2026-11-29
In This Lesson
  • Understand why teams never build new features directly on main
  • Create and switch between isolated branches using git switch -c
  • Merge finished branches cleanly into main and delete old branches

Imagine your website is live and real visitors are using it today. Now you want to redesign the header—a change that will take 3 days and might temporarily break the layout while you experiment.

If you save half-finished edits directly onto main, anyone else pulling main (or deploying your site) gets broken code! Branches solve this by letting you open a safe, parallel workspace where you can build and test freely while main stays 100% stable.

Creating & Switching Branches

Git Branch Isolation: Fast-Forward vs 3-Way Merge
Fast-Forward Merge (main untouched)
C1C2C3 (feat)

main pointer simply slides forward from C2 to C3—linear history, zero extra commit.

3-Way Merge (both branches moved)
C2 (base)C4 (main)+C3 (feat)M5

Git combines both tips into a new merge commit M5 with two parents.

Feature branches isolate work in progress. When merging back into main, Git fast-forwards if main did not move, or creates a 3-way merge commit if both diverged.

Think of a branch like making a temporary side lane off a highway. Every commit you make inside that side lane stays isolated from main:

bash
git switch -c feat/header-redesign   # Create and switch to the new branch
git branch                           # List local branches (* marks your active one)

Notice the naming habit feat/header-redesign. Teams often prefix branches with feat/ (new feature), fix/ (bug fix), or docs/ (documentation) so anyone can tell what a branch is doing at a glance.

While working on feat/header-redesign, you edit files and run git add + git commit as usual. If an urgent fix comes in on main, you can hop back to an untouched main in one second:

bash
git switch main

Merging Finished Work Back Into main

Once your feature is complete, tested, and committed on its branch, bring those commits back into main in three steps:

bash
git switch main                     # 1. Stand on the receiving branch (main)
git merge feat/header-redesign      # 2. Pull the feature commits into main
git branch -d feat/header-redesign  # 3. Clean up the finished branch

Why -d instead of -D? Lowercase -d is a safe delete—Git refuses to delete the branch if you haven't merged its commits yet, protecting you from losing work by accident.

Fast-Forward vs 3-Way Merge

If nobody touched main while you worked on your branch, Git slides the main pointer straight forward to match your latest commit (fast-forward merge). If teammates also merged other commits onto main in the meantime, Git automatically creates a clean merge commit combining both timelines together!

Starting a New Feature Without Switching Back to main First

Suppose you finish feat/navbar and immediately run git switch -c feat/footer while still standing on feat/navbar. Now your new footer branch accidentally inherits all your navbar changes! Always run git switch main first so every new feature branches cleanly off main.

Interactive Walkthrough: git merge vs git rebase

Many teams also use git rebase main while working on a private feature branch. Instead of creating a merge commit, rebase replays your feature commits cleanly on top of the latest main, keeping project history strictly linear:

Interactive Walkthrough: Branch Divergence & git rebase mainStep 1 / 4
main = C2feat/nav = C2HEAD → main
C1
C2 (main, HEAD)

Step 1: Start on main (C2)

Your repo has two commits on main. HEAD points to main.

bash
# On your personal feature branch (not yet shared with teammates):
git fetch origin
git rebase origin/main
The Golden Rule of Rebasing

Only rebase your own unshared feature branches. Never run git rebase on main or any branch teammates have already pulled—rewriting shared commit hashes forces teammates into messy history conflicts.

Quick Checkpoints

Check Your Understanding

Which command creates a new branch named fix/navbar and switches into it immediately?

Check Your Understanding

Why is git branch -d safer than git branch -D when deleting an old branch?