git switch Command Guide
Switch branches
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git switch <branch>Switch to branchgit switch -Switch to previousgit switch -c <name>Create and switchCreate
-c <name>Create new branch-C <name>Force create (reset)--orphan <name>Create orphan branchOptions
--detachDetached HEAD mode--trackTrack remote branch--discard-changesDiscard local changesDownloadable Image Preview
Basic Usage
The git switch command (Git 2.23+) is a clearer alternative to git checkout for switching branches.
# Switch to an existing branch
git switch main
# Switch to previous branch
git switch -
# Create and switch to new branch
git switch -c feature-xCreating Branches
# Create and switch to new branch
git switch -c feature/login
# Create from specific commit
git switch -c hotfix abc1234
# Create tracking a remote branch
git switch -c feature --track origin/feature
# Force create (reset if exists)
git switch -C feature-x
# Create orphan branch (no history)
git switch --orphan docsCommon Options
| Option | Description |
|---|---|
| -c <branch> | Create a new branch and switch to it |
| -C <branch> | Create or reset branch and switch |
| --detach | Switch to commit in detached HEAD |
| --track | Set up tracking for remote branch |
| --discard-changes | Discard local changes when switching |
Switch vs Checkout
# These are equivalent:
git checkout main
git switch main
git checkout -b feature
git switch -c feature
git checkout --track origin/feature
git switch --track origin/feature
# But switch won't restore files:
git checkout -- file.js # Restore file
git switch -- file.js # Error! Use git restoreWhy Use switch?
- • Clearer intent - only switches branches
- • Safer - won't accidentally modify files
- • Requires explicit --detach for detached HEAD
- • Part of the modern Git workflow
Practical Examples
# Daily workflow
git switch main
git pull
git switch -c feature/new-thing
# Toggle between branches
git switch feature
git switch - # Back to main
git switch - # Back to feature
# Track remote branch
git fetch origin
git switch -c feature --track origin/feature
# Discard changes and switch
git switch --discard-changes mainFrequently Asked Questions
What is the difference between git switch and git checkout?
git switch (Git 2.23+) only handles branches, making it safer and clearer. git checkout does both branches and files, which can be confusing. Use switch for branches, restore for files.
How do I switch and create a branch in one command?
Use "git switch -c <branch-name>" to create a new branch and switch to it in one command. This is equivalent to "git checkout -b <branch-name>".
What happens if I have uncommitted changes when switching?
Git will prevent switching if your changes would be overwritten. Either commit, stash, or use --discard-changes to discard them.
Can I use git switch to detach HEAD?
Yes, use "git switch --detach <commit>" to enter detached HEAD state. Unlike checkout, switch requires the explicit --detach flag.
Summary
git switch is the modern, clearer way to switch branches. Use it instead of git checkout for branch operations.
Quick Reference
git switch <branch>- Switch to branchgit switch -c <name>- Create and switchgit switch -- Previous branchgit switch --detach <commit>- Detached HEAD
Official Documentation
For authoritative information, refer to the official documentation: