Switching Branches
Move between branches using git checkout or git switch.
Checkout vs Switch
Git offers two commands for switching branches:
| Command | Purpose |
|---|---|
git checkout | Traditional, multi-purpose |
git switch | Newer, branch-focused |
Switching Branches
Basic Switching
Using checkout
git checkout feature
Using switch (recommended)
git switch feature
Create and Switch
Using checkout
git checkout -b new-feature
Using switch
git switch -c new-feature
What Happens When You Switch
- Git updates HEAD to point to new branch
- Updates working directory to match branch
- Updates staging area
Switching with Uncommitted Changes
If you have uncommitted changes:
- Git may prevent switching (if conflicts would occur)
- Or carry changes to the new branch
Options:
# Stash changes first
git stash
git checkout other-branch
git stash pop
# Or commit first
git commit -m "WIP"
git checkout other-branch
Detached HEAD State
Checking out a commit (not a branch) creates "detached HEAD":
git checkout abc1234 # Detached HEAD
In this state:
- You can look around
- Making commits creates orphaned commits
- Create a branch to save work:
git checkout -b new-branch
Exercise: Switch Branches
Create a branch called develop and switch to it:

