Lesson 17 +15 XP

Switch and Checkout

Switch and Checkout

Creating a branch is not enough. You also need to switch to it to start working there.

Switch to a branch

git checkout feature-login

This is the classic command. It moves you onto the feature-login branch, and your files update to match that branch.

The modern way

git switch feature-login

git switch was added to make switching clearer. It only switches branches, nothing else.

Create and switch in one step

git switch -c new-feature

The -c flag creates the branch and switches to it in one command.

The same with checkout

git checkout -b new-feature

-b does the same as -c.

Which should you use?

Both work. Modern guidance is:

  • Use git switch to change branches.
  • Use git checkout for older muscle memory or restoring files.

TL;DR

  • git checkout branch or git switch branch moves to a branch.
  • git switch -c name creates and switches.
  • git checkout -b name is the older form.
  • Your working files update to match the branch you switch to.