How to create a new branch and switch to it?
git switch -c new_branch
To create a new branch and switch to it in one command use git switch -c new_branch
, where new_branch is the name of the new branch.
The switch command was introduced with git version 2.23. In older versions of git, you will need to use git checkout
.
git status
On branch main
nothing to commit, working tree clean
git switch -c new_branch
Switched to a new branch 'new_branch'
git status
On branch new_branch
nothing to commit, working tree clean
See git switch to learn more about it.