Create and switch branch

2 snippets in Git

Also written as create branch and switch

GitGit

Switch Branches

Git · Branching
syntax
git switch <branch>
git switch -c <new-branch>
example
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.

Checkout a Branch (Classic)

Git · Branching
syntax
git checkout <branch>
git checkout -b <new-branch>
example
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.