How to create and switch branches in Git

Run git switch -c <name> to create a branch and switch to it in one step. Use git switch <name> to move between branches and git branch to list them.

Step by step

  1. Create a branch and switch to it:

    $ git switch -c dark-mode
  2. List your branches (* is the current one):

    $ git branch
  3. Switch back:

    $ git switch main
  4. Older equivalent:

    $ git checkout -b dark-mode

A branch is just a label pointing at a commit. HEAD tells you which branch you're on; new commits move that branch forward.

Try it in the interactive sandbox →Practice “Create a branch” with a real repository and teammates, right in your browser.

Related guides