branch and switch
Switching is constant in real work. You pause a half-done feature to review a teammate's branch, hop to main for an urgent fix, then come back, often five times before lunch. That daily traffic is why the commands are short.
Three of them cover the whole job.
$ git branch new-sauce $ git switch new-sauce Switched to branch 'new-sauce' $ git switch -c quick-idea Switched to a new branch 'quick-idea'
git branch new-sauce creates a label at your current commit without moving you, git switch new-sauce moves HEAD onto that branch, and git switch -c quick-idea does both in one step.
Run git branch with no arguments and it lists your branches instead, marking the current one with *.
$ git branch main new-sauce * quick-idea
Commits made from here move the quick-idea label forward while main stays planted exactly where it was, which is the isolation the whole unit is built on. When the experiment works out, lesson 5-3 shows how to bring it home.
Older tutorials use git checkout new-sauce and git checkout -b quick-idea for the same two actions. They still work. switch was added because checkout had accumulated too many unrelated jobs, moving between branches and also throwing away file changes, and one command that both navigates and destroys is a bad idea.
A recorded session
This session creates a branch, looks around, and comes back.
Each step below shows the command and the output it printed.
Step 1. Create a branch called new-sauce and switch onto it with one command.
~/recipe-book $ git switch -c new-sauce
Switched to a new branch 'new-sauce'Note the wording, "a new branch", which is Git confirming it created the label rather than finding one.
Step 2. List your branches and find the * marking where you stand.
~/recipe-book $ git branch main * new-sauce
Step 3. Switch back to main.
~/recipe-book $ git switch main
Switched to branch 'main'This time the message says "branch" without "new", since main already existed. Both branches still point at the same commit at this stage, because no commits have been made yet, and they only diverge once you commit while standing on one of them.
What switching does to your files
Switching branches rewrites your working directory to match the snapshot the target branch points at. Files change on disk immediately.
Switch to a branch from before a file existed and the file disappears from the folder, then switch back and it returns. Nothing is lost, since snapshots are permanent, but it startles everyone the first time they see a file vanish from their editor's sidebar.
Uncommitted changes get special treatment. If the target snapshot would overwrite them, Git refuses to move at all.
$ git switch main
error: Your local changes to the following files would be overwritten by checkout:
pancakes.txt
Please commit your changes or stash them before you switch branches.This is the protective refusal pattern from earlier units, and as usual Git names your options in the message itself. Either commit the work, which makes it part of the branch you are leaving, or stash it, a tool waiting in lesson 10-1.
Worth noticing that Git allows the switch when your uncommitted edits are in files the target snapshot does not touch. The check is specific rather than blanket, which is why switching with a dirty working directory often just works.
git switch -c fix-typo creates a branch called fix-typo and switches onto it in one step.
The -c stands for create. It bundles git branch fix-typo and then git switch fix-typo into a single command.
This is how nearly every piece of work starts in practice, which is why the shortcut exists at all. The new branch is created at your current commit, so fix-typo and whatever you were on both point at the same snapshot until you make the first commit.
When you switch from photos to main and main's snapshot has no gallery.txt, the file disappears from your folder, and switching back to photos brings it right back.
The working directory always mirrors the branch you are on, so a file that does not exist in the target snapshot cannot exist on disk after the switch.
Nothing has been lost in the process. gallery.txt lives inside the photos branch's commits, which is permanent storage, and the folder is only ever a view of one snapshot at a time. This is worth internalizing early, because the instinct on seeing a file vanish is to panic and recreate it, which then produces a genuine conflict later.
The single command is git switch -c dark-mode.
It creates the branch at your current commit and moves HEAD onto it, so the next commit you make extends dark-mode and leaves the branch you came from untouched.
The older equivalent, git checkout -b dark-mode, does exactly the same thing and appears throughout existing documentation and team scripts, so both are worth recognizing even though switch is the form to reach for now.