Git 2 reference · Branching and Merging

git switch Command (Git)

Switch to another branch, or create one and switch to it with -c; the modern replacement for git checkout branches.

Git 2

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

git switch BRANCH   git switch -c NEW [START]

Parameters

ParameterDescription
-c NEW Create and switch.
- Back to the previous branch.
--detach COMMIT Look at an old commit without a branch.

Examples

Create, switch and go back
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
git switch -q -c feature/cart
git branch --show-current
git switch -q -
git branch --show-current

Output

feature/cart
main

Run this on your computer — see “Get Started” for setup.

Uncommitted changes come with you
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
echo draft > draft.txt
git switch -q -c experiment
git status -s

Output

?? draft.txt

Run this on your computer — see “Get Started” for setup.

Was this page helpful?