Currently Available: Need a skilled Software Developer for your next project?
Categories
Git

Stop Using git checkout

Git 2.23 introduced two commands that replace the two different jobs git checkout used to do:

  • git switch — change or create branches
  • git restore — restore/discard file contents from the index or another commit

Use these instead of git checkout for clarity and safety.

What actually changed

git checkout tried to do both “move to another branch” and “change files.” That led to ambiguity:

  • git checkout foo — is foo a branch or a path?
  • git checkout -- file — same command, totally different action

Splitting the responsibilities makes intent explicit and reduces accidental overwrites.

Task mapping

Task Old (checkout) New (preferred)
Switch to an existing branch git checkout my-branch git switch my-branch
Create and switch to a branch git checkout -b feature git switch -c feature
Return to previous branch git checkout - git switch -
Discard local changes in a file git checkout -- path/to/file git restore path/to/file
Restore a file from a commit git checkout <commit> -- file git restore --source=<commit> file
Detach HEAD at a commit git checkout <commit> git switch --detach <commit>

Typical commands

# Branch operations
git switch feature/login
git switch -c hotfix/logo
git switch -

# File restoration
git restore app/Console/Commands/ImportSalesforce.php
git restore --source=abc123 src/util.js

When you might still use git checkout

  • You are on Git < 2.23.
  • Legacy scripts/aliases still depend on it and haven’t been updated.

Otherwise, standardize on git switch and git restore.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Subscribe to my newsletter

Get new posts when I publish them.

I respect your privacy. Unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *