branching

git switch

Modern, focused command (Git 2.23+) that only switches branches — no file restoration overloading like checkout. Safer and clearer for everyday branch changes.

git switch [<options>] <branch>

Common flags

Flag Purpose
-c <new> Create a new branch and switch to it
-C <new> Create or reset the branch and switch
--detach Switch to a commit in detached HEAD mode
--discard-changes Throw away local modifications while switching
- Switch back to the previously checked-out branch

Examples

git switch main

Switch to the main branch

git switch -c feature/auth

Create and switch to a new feature branch

git switch -

Toggle back to the previous branch

Gotcha

'switch' won't touch files by design; if you meant to restore a file's contents, use 'git restore' instead.

Related commands

← All Git commands · Git cheat sheet · Git rebase vs merge