Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git switch: change branches the easy way

git switch is the modern command for navigating between branches. Clearer and safer than checkout, it's the recommended approach since Git 2.23.

Modern

What is git switch?

git switch does one thing and does it well: switch branches. It's the modern and clear version of git checkout for branch navigation.

Introduced in Git 2.23, switch cleanly separates branch navigation (switch) from file restoration (restore).

Git switch syntax

git switch branch

Switch to an existing branch

git switch -c new-branch

Create and switch to a new branch

git switch -

Go back to the previous branch

Git switch in practice

Navigate between branches easily.

Basic switch

Advanced switch

git switch vs git checkout

git switch

Only for branches. Clear and unambiguous. Refuses dangerous operations by default.

Recommended for beginners and pros alike

git checkout

Branches + files + commits. Versatile but confusing. Can overwrite files by accident.

Legacy but still functional

Common mistakes with git switch

Uncommitted changes

Switch will refuse to change branches if you have uncommitted changes that would be overwritten. Run git stash or commit first.

Branch doesn't exist

If the branch doesn't exist locally, use git switch -c to create it. If it exists on the remote, git switch will automatically create it and set up tracking.

Confusing -c and -C

-c creates a new branch (fails if it exists). -C creates or resets the branch (overwrites if it exists).

Part of the Git Branching Workflow guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git switch

Navigate between branches with GitQuest

GitQuest investigations teach you to master branch navigation.

Start the investigations