Skip to content

Git Switch

In one line

git switch is the modern (Git 2.23+) command for changing or creating branches, replacing git checkout for branch operations.

In simple words

git switch is the modern command for changing the current branch, introduced in Git 2.23 to split the overloaded git checkout into two clearer commands: switch (for branches) and restore (for files). switch only moves HEAD and updates the working tree to match the target branch — it does not touch individual files.

The common forms: git switch main checks out the main branch. git switch -c feature creates and switches to a new branch. git switch - (with a single dash) switches back to the previous branch, like cd -. You can also detach HEAD with --detach to inspect a specific commit without a branch.

switch is clearer and safer than checkout for branch operations because it refuses to switch if local changes would be overwritten (instead of silently carrying them over like checkout sometimes did). For new code and tutorials, prefer switch + restore over checkout.

Example

bash
# Switch to an existing branch
git switch main

# Create and switch to a new branch
git switch -c feature/login

# Switch back to the previous branch
git switch -

# Create a new branch starting from a different point
git switch -c hotfix main

# Detach HEAD to inspect an old commit
git switch --detach v1.2.0

switch changes branches. -c creates first. - goes to the previous branch. --detach inspects a commit without a branch. It refuses if changes would be lost.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary