git switch feature/user-profile
# Create and switch in one step:git switch -c feature/payments
Output
Switched to branch 'feature/user-profile'
Note git switch is the modern replacement for git checkout (for branch switching). The -c flag creates the branch if it doesn't exist. You'll get an error if you have uncommitted changes that conflict with the target branch.
git checkout main
git checkout -b bugfix/login-error
Output
Switched to branch 'main'
Note git checkout does double duty: switching branches AND restoring files. The newer git switch (branches) and git restore (files) commands split this up for clarity. Both approaches still work.
Frequently asked questions
How do you create and switch branch?
Git covers this with 2 copy-ready snippets on this page. The "Switch Branches" snippet in Git uses `git switch <branch>`.
Which command does the Git example use?
The "Switch Branches" snippet uses `git switch <branch>`, from the Branching section of the Git cheat sheet.
What other Git snippets are shown for "create and switch branch"?
Besides "Switch Branches", this page also shows "Checkout a Branch (Classic)".
Is there anything to watch out for?
Yes. For "Switch Branches": git switch is the modern replacement for git checkout (for branch switching). The -c flag creates the branch if it doesn't exist. You'll get an error if you have uncommitted changes that conflict with the target branch.