TAKASHI YAMASHINA software engineer

Create and Switch Branches

The git switch -c command creates a new branch and switches to it simultaneously. It is superior to the combination of git branch and git checkout, or git checkout -b.

Usage

bash
# Create a new branch `feat/new-feature` from the current branch
git switch -c feat/new-feature
# Create a new branch `feat/new-feature` from the `main` branch
git switch -c feat/new-feature main
# Create a new branch `feat/new-feature` from the remote `origin/main` branch
git switch -c feat/new-feature origin/main

Why git switch -c is Better Than git checkout -b

1. Clear Intent

git switch clearly expresses the intent to "switch branches." git checkout has multiple uses including branch switching and file restoration, which can make the intent ambiguous.

2. More Intuitive Command Name

The word switch clearly means "to change," making it easy for beginners to understand.

3. Modern Git Recommended Method

git switch was introduced in Git 2.23 (2019) as a relatively new command recommended by the Git development team. It was designed to address the complexity of git checkout.

4. Helpful Error Messages

When you make a mistake with an existing branch name, git switch displays more understandable error messages.

bash
# When you make a mistake with an existing branch name
git switch existing-branch
# Error: branch 'existing-branch' not found. Use '-c' option to create it.