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 cover it:
$ git branch new-sauce # create a label here (doesn't move you) $ git switch new-sauce # move HEAD onto that branch Switched to branch 'new-sauce' $ git switch -c quick-idea # create AND switch, one step Switched to a new branch 'quick-idea'
git branch with no arguments lists your branches, marking your current one with *:
$ git branch main new-sauce * quick-idea
Commits you make now move the quick-idea label forward while main stays planted. When the experiment works, unit 5-3 shows how to bring it home.
(In older tutorials you'll see git checkout new-sauce and git checkout -b quick-idea. Same actions, older command. switch was added because checkout did too many unrelated things.)
Create a branch, look around, and come back.
Step 1/3: Create a branch called new-sauce and switch onto it, one command.
What switching does to your files
Switching branches rewrites your working directory to match the snapshot the target branch points to. Files change on disk instantly. Switch to a branch from before a file existed and the file disappears from the folder, switch back and it returns. Nothing is lost, snapshots are permanent, but it surprises everyone the first time.
What about uncommitted changes when you switch? If they'd be overwritten by the target snapshot, Git refuses:
$ 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.Git protects you and, as always, names your options: commit the work, or stash it, a tool waiting in lesson 10-1.
Quiz
What does git switch -c fix-typo do?
Quiz
You're on branch photos where you committed gallery.txt. You run git switch main, and main's snapshot has no gallery.txt. What happens to the file in your folder?
Problem
Which single command creates a branch named dark-mode and moves you onto it? (Write the full command.)