Table of Contents

Introduction

git switch -c is how a piece of work starts in modern Git: make a branch, be on it, one command. It's the same operation as git checkout -b, spelled with the branch-only command that Git 2.23 introduced.

In this article, we'll:

  1. Watch git switch -c hotfix create a branch and move HEAD onto it
  2. Check what changed and what didn't
  3. Look at branching from an older commit

What is git switch -c?

git switch -c <name> creates a new branch pointing at the current commit and switches to it. Under the hood that's a new file in .git/refs/heads/ and a one-line change to .git/HEAD. Because the new branch starts at the commit you're already on, no files change.

Watch it happen

Our sample repo has a new branch created from the current commit. Here's git switch -c hotfix:

  1. Before: HEAD is attached to main at 8c02d5b. There is no hotfix branch yet.
  2. Git creates the hotfix branch at 8c02d5b, alongside main, and attaches HEAD to it.
  3. main doesn't move, and no commits are created.

Before and after

After the command, two branches share the commit 8c02d5b, HEAD is on the new one, and git status says hotfix. From here, the next commit moves hotfix forward and leaves main behind, which is the whole point.

The raw git output, if you want to read along in text

git log --oneline --graph --all

before

* 1117a34 (feature) Add search tests
* e5869f0 Fix typo in search box
* fc19889 Add search box
| * 8c02d5b (HEAD -> main) Update dependencies
| * a0b2db3 Add user settings page
|/  
* 96c4fc2 Fix header layout
* ae65976 Add login page
* 3e1ffe4 Add project skeleton
* 4114b2c Initial commit

after

* 1117a34 (feature) Add search tests
* e5869f0 Fix typo in search box
* fc19889 Add search box
| * 8c02d5b (HEAD -> hotfix, main) Update dependencies
| * a0b2db3 Add user settings page
|/  
* 96c4fc2 Fix header layout
* ae65976 Add login page
* 3e1ffe4 Add project skeleton
* 4114b2c Initial commit

what git printed

Switched to a new branch 'hotfix'

Starting from another commit

git switch -c hotfix 96c4fc2

creates the branch at 96c4fc2 instead, and switches to it. Your working directory changes to match that commit, so this form does touch files.

One habit I picked up is to create the branch before making the first edit, not after. Commits made on main by accident are easy enough to move, but it's one more thing to fix, and git switch -c is short enough that there's no reason to wait.

Is it safe?

Safe git-sim pre-flight

Switches to 'hotfix'.

Creating and switching at the current commit changes one ref file and HEAD. Uncommitted changes carry over untouched.

How to undo it

git switch main
git branch -d hotfix

The branch points at the same commit as main, so Git deletes it as merged.

Try it on your repository

pip install git-sim
git-sim switch -c hotfix

git-sim shows the new label and the HEAD move on your own graph.

Common questions

What does git switch -c do?

Creates a branch at the current commit and switches to it. -c stands for create. The git switch documentation also lists -C, which resets a branch that already exists instead of refusing.

What is the difference between git switch -c and git checkout -b?

Nothing in effect. switch -c is the newer spelling, introduced when Git split checkout into switch and restore.

How do I create a branch from a specific commit with git switch?

git switch -c <name> <commit>.

Does git switch -c change my files?

Not when branching from the current commit. Branching from another commit updates the working directory to match it.

Summary

In this article, we watched git switch -c create a hotfix branch on the current commit and move HEAD onto it, confirmed nothing else changed, and saw how to start the branch from an older commit.

Next steps

git commit is the next thing you'll do on the new branch, and git merge is how the work gets back.