Switching Branches

git switch moves you to another branch: Git points HEAD at it and updates the files in your working tree to match that branch's latest commit. New commits then go onto that branch.

Switching

$
git switch desserts
Output
Switched to branch 'desserts'
$
echo "Brownies: chocolate, butter, sugar" > brownies.txt
git add brownies.txt
git commit -m "Add brownie recipe"
ls
Output
[desserts 3cf4499] Add brownie recipe
 1 file changed, 1 insertion(+)
 create mode 100644 brownies.txt
brownies.txt  omelette.txt  pancakes.txt

Switch back to main and watch the brownie recipe disappear — it only exists on desserts:

$
git switch main
ls
Output
Switched to branch 'main'
omelette.txt  pancakes.txt

Nothing is lost: switching back to desserts brings the file back. Your working tree always shows the snapshot of the branch you're on.

Create and switch in one step

-c (create) is how you'll usually start new work:

$
git switch -c soups
git branch
Output
Switched to a new branch 'soups'
  desserts
  main
* soups

git switch - jumps back to the previous branch, like cd - in the terminal:

$
git switch -
Output
Switched to branch 'main'

The older command: git checkout

Before Git 2.23, switching was done with git checkout, which also restores files. It still works and you'll see it everywhere:

ModernOlder equivalent
git switch dessertsgit checkout desserts
git switch -c soupsgit checkout -b soups
git switch -git checkout -
git switch --detach a1b2c3dgit checkout a1b2c3d

Uncommitted changes when switching

Changes you haven't committed don't belong to any branch — they're just in your working tree. If they don't clash with the other branch, Git carries them along:

$
echo "Pancakes: flour, milk, eggs, vanilla" > pancakes.txt
git switch desserts
Output
M	pancakes.txt
Switched to branch 'desserts'

The M line shows the edit came along. But if the other branch has a different version of a file you've changed, switching would overwrite your work, so Git stops:

$
echo "Pancakes: flour, milk, eggs, blueberries" > pancakes.txt
git switch desserts
Output
error: Your local changes to the following files would be overwritten by checkout:
	pancakes.txt
Please commit your changes or stash them before you switch branches.
Aborting

You have three ways out: commit the change first, put it aside with git stash (lesson 29), or discard it with git restore. Building the habit of committing or stashing before switching avoids the problem entirely.

Detached HEAD

You can also switch to a specific commit instead of a branch — to look at an old version or test something there:

$
git switch --detach HEAD~1
Output
HEAD is now at c4f5e47 Add pancake recipe
$
git status
Output
HEAD detached at c4f5e47
nothing to commit, working tree clean

This state is called detached HEAD: HEAD points straight at a commit rather than at a branch. Looking around is perfectly safe. But if you make commits here, no branch points at them, and once you switch away they become hard to find. If you want to keep work you did while detached, give it a branch before leaving:

$
echo "Crepes: flour, milk, eggs, butter" > crepes.txt
git add crepes.txt
git commit --quiet -m "Try a crepe recipe"
git switch -c crepe-experiment
Output
Switched to a new branch 'crepe-experiment'

Or just go back to a branch to leave detached HEAD:

$
git switch main
Output
Switched to branch 'main'

Branch name completion

Typing branch names gets tedious. Git Bash on Windows and Git installed via Homebrew include tab completion: type git switch des and press Tab. On Linux it's usually enabled by the bash-completion package; for zsh it's built in.

Try it yourself

Create a branch with git switch -c, commit a new file on it, switch back and forth with git switch -, and confirm with ls that the file appears and disappears. Then check out an old commit in detached HEAD mode and return to main.