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:
- HEAD moves to the target branch
- Working tree files are updated
- 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
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
# ─── 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
| Task | Modern (2.23+) | Legacy |
|---|---|---|
| Switch branch | git switch branch | git checkout branch |
| Create + switch | git switch -c branch | git checkout -b branch |
| Previous branch | git switch - | git checkout - |
| Restore a file | git restore file | git checkout -- file |
git switchwas introduced becausegit checkoutdid too many things (branch switching + file restoration). Usegit switchfor 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 switchreplaced the branch-switching role ofgit checkoutin 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 likecd -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.