User profile picture

Git switch vs git checkout (and when you still need checkout)

git checkout was the workhorse of Git for years—it switched branches, restored files, and even created branches all in one command. That flexibility made it powerful but dangerous. So Git split it into two specialized commands: git switch and git restore.

git switch — Navigate branches safely

  • git switch main — switch to an existing branch
  • git switch -c feature/new-login — create and switch to a new branch
  • git switch - — switch to the previous branch

git restore — Undo changes to files

  • git restore index.html — discard unstaged changes
  • git restore --staged index.html — unstage a file
  • git restore . — discard all local changes

This separation prevents accidents. Try git switch on a dirty working directory and Git will refuse—protecting you from losing uncommitted work. git checkout would let you switch and silently lose those changes.

When you still need git checkout:

  1. Restoring a single file from a specific commit:

    git checkout abc1234 -- path/to/file.txt

    This pulls an old version of one file. git restore can’t do this in one command.

  2. Creating and switching to a new branch from a specific starting point:

    git checkout -b new-branch old-branch

    While git switch -c creates from HEAD, sometimes you need to branch from a different starting point.

  3. Checking out a specific file from another branch without switching:

    git checkout main -- config.yml

    This merges just that one file from another branch.

  4. Older Git versions — if you’re on a system with Git < 2.23, you don’t have switch/restore.

For day-to-day work, git switch and git restore are the safer default. Keep git checkout in your back pocket for these edge cases.

Tags:

# git

# version control

# tooling