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 branchgit switch -c feature/new-login— create and switch to a new branchgit switch -— switch to the previous branch
git restore — Undo changes to files
git restore index.html— discard unstaged changesgit restore --staged index.html— unstage a filegit 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:
-
Restoring a single file from a specific commit:
git checkout abc1234 -- path/to/file.txtThis pulls an old version of one file.
git restorecan’t do this in one command. -
Creating and switching to a new branch from a specific starting point:
git checkout -b new-branch old-branchWhile
git switch -ccreates from HEAD, sometimes you need to branch from a different starting point. -
Checking out a specific file from another branch without switching:
git checkout main -- config.ymlThis merges just that one file from another branch.
-
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.
