How do you create and switch to a new branch in Git?
Create a branch and check it out in one step with git switch -c or the older git checkout -b.
GitVersion Control
Overview
Branches let you work on features or fixes in isolation without affecting the main codebase. Creating and switching to a new branch is a daily Git operation.
Key Points
- •`git switch -c <name>` is the modern way (Git 2.23+): c stands for "create".
- •`git checkout -b <name>` is the older equivalent both do the same thing.
- •Branch names should be short, lowercase, and use hyphens: `feature/login`, `fix/navbar-bug`.
- •To create a branch from a specific commit or tag, pass it as a second argument.
- •Use `git branch` to list all local branches; `git branch -a` includes remotes.
Examples
Create and switch to a new branch (modern syntax)
bashgit switch -c feature/user-authRecommended for Git 2.23 and later.
Create and switch to a new branch (classic syntax)
bashgit checkout -b feature/user-authWorks in all Git versions. Still widely used.
Create a branch from a specific starting point
bash# from a commit hash
git switch -c hotfix/button-fix abc1234
# from an existing branch
git switch -c release/v2 mainList all branches and push the new branch to remote
bashgit branch # list local branches
git branch -a # include remote branches
# push to remote and set upstream
git push -u origin feature/user-authPro Tip
Prefer `git switch` and `git restore` over `git checkout` in modern Git. The old `checkout` command does too many things (switching branches, restoring files) which makes it easy to run the wrong variant by accident.