Lesson Completion
Back to course

Switching Branches

Beginner
9 minutes4.8Git

The Hook (The "Byte-Sized" Intro)

Switching branches in Git is like teleporting between parallel universes. One second you're looking at the login feature, the next you're on the payment page — different files, different changes, same machine. Your working tree transforms instantly to match the branch. It feels like magic, but it's just pointers.

📖 What is Switching Branches?

Switching branches moves HEAD to point to a different branch. Git then updates your working tree and staging area to match the snapshot at that branch's tip. It's fast because Git only changes the files that differ between branches.

Conceptual Clarity

  • git switch <branch> is the modern, clear command (Git 2.23+)
  • git checkout <branch> is the legacy command — still works but overloaded
  • When you switch, Git updates your working tree to match the target branch
  • Uncommitted changes can block a switch if they conflict with the target branch
  • You can stash uncommitted changes before switching to avoid conflicts

What happens when you switch:

  1. HEAD moves to the target branch
  2. Working tree files are updated
  3. Staging area is reset to match the target branch's commit

Real-Life Analogy

Switching branches is like having multiple desks in your office, each with a different project. You walk to a different desk (git switch) and everything on it — the files, the notes, the state of the work — is exactly as you left it last time.

Visual Architecture

flowchart LR subgraph BEFORE["Before Switch"] H1["HEAD → main"] M["main: a1b2c3d"] F["feature: e4f5g6h"] end subgraph AFTER["After: git switch feature"] H2["HEAD → feature"] M2["main: a1b2c3d"] F2["feature: e4f5g6h"] end BEFORE -->|"git switch feature"| AFTER style BEFORE fill:#1a1a2e,stroke:#e94560,color:#e94560 style AFTER fill:#0f3460,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Multi-tasking: Jump between features, bugfixes, and reviews instantly.
  • Clean context: Each branch has its own file state — no mixing unrelated changes.
  • Safety: Git warns you if switching would overwrite uncommitted work.
  • Speed: Switching is nearly instant since Git only updates changed files.

Code

bash
# ─── Switch to an existing branch (modern) ─── git switch feature/login # Output: Switched to branch 'feature/login' # ─── Switch to an existing branch (legacy) ─── git checkout feature/login # ─── Create and switch in one command ─── git switch -c feature/new-idea # Output: Switched to a new branch 'feature/new-idea' # ─── Switch back to the previous branch ─── git switch - # The dash means "the branch I was just on" — great shortcut! # ─── Switch when you have uncommitted changes ─── git switch feature/login # Error: Your local changes would be overwritten by checkout. # Fix 1: Commit your changes first git commit -am "WIP: save progress" # Fix 2: Stash your changes git stash git switch feature/login # ... do work ... git switch - git stash pop # Restore your stashed changes # ─── Force switch (discard all uncommitted changes) ─── git switch -f main # ⚠️ This throws away uncommitted changes — use with caution!

git switch vs git checkout

TaskModern (2.23+)Legacy
Switch branchgit switch branchgit checkout branch
Create + switchgit switch -c branchgit checkout -b branch
Previous branchgit switch -git checkout -
Restore a filegit restore filegit checkout -- file

git switch was introduced because git checkout did too many things (branch switching + file restoration). Use git switch for clarity.

Key Takeaways

  • git switch <branch> changes branches and updates your working tree.
  • Use git switch - to quickly toggle back to your previous branch.
  • Uncommitted changes can block switching — stash or commit them first.
  • git switch replaced the branch-switching role of git checkout in Git 2.23.

Interview Prep

  • Q: What happens to your working tree when you switch branches? A: Git updates the working tree files to match the snapshot at the target branch's tip. Only files that differ between branches are changed. If you have uncommitted changes that conflict, Git refuses the switch.

  • Q: What does git switch - do? A: It switches to the previously checked-out branch. It's a shortcut that works like cd - in the shell — great for toggling between two branches rapidly.

  • Q: What should you do with uncommitted changes before switching branches? A: Either commit them (git commit), stash them (git stash), or discard them (git restore .). Git refuses to switch if uncommitted changes conflict with the target branch to prevent data loss.

Topics Covered

Git BranchingGit Fundamentals

Tags

#git#switch#checkout#beginner-friendly

Last Updated

2026-02-12