Back to roadmap

Lesson 06

Checkout

Checkout selects an existing branch to work on: it moves HEAD and updates tracked files to match that branch.

Visual Explanation

Ready to run git checkout feature/login.

Keep These Ideas

Select, Don't Rebuild
Checking out an existing branch selects it; the branch and its commits already exist.
HEAD Moves
HEAD identifies the branch or commit you currently have checked out.
Files Follow
Tracked Working Directory files update to match the selected branch when it is safe.

The Real Command

Terminal
git checkout feature/login

This switches to the existing feature/login branch and updates your project files to that branch's version.

Command Details

git switch feature/login

Modern, focused branch switching. switch is the clearer modern command when your only goal is to change branches.

git checkout -b feature/search

-b creates and checks out a branch. Use a new branch name that does not already exist.

git status --short --branch

--branch confirms the current branch. The concise header shows where HEAD is attached.

Example Output
## feature/login
git checkout -- app.js

This older form restores app.js. It discards unstaged edits. Modern Git offers git restore app.js for this job.

git checkout a1b2c3d

Checking out a commit detaches HEAD. You can inspect an old checkpoint, but new work needs a branch if you want to keep it easily.

Abridged Example Output
Note: switching to 'a1b2c3d'.

You are in 'detached HEAD' state.
HEAD is now at a1b2c3d Add navigation

Knowledge Check

Move Between Lines of Work

Select an existing line of work, understand HEAD, and recognize checkout's older multi-purpose forms.

Question 1 of 5

  1. Question 1: current
  2. Question 2: not started
  3. Question 3: not started
  4. Question 4: not started
  5. Question 5: not started
Command Challenge

Switch from main to the existing feature/login branch.