Quickly Restore All Deleted Files in Git
If you’ve accidentally deleted multiple tracked files in your Git repository, you can bring them all back at once using this simple…
Git 2.23 introduced two commands that replace the two different jobs git checkout used to do:
git switch — change or create branchesgit restore — restore/discard file contents from the index or another commitUse these instead of git checkout for clarity and safety.
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 actionSplitting the responsibilities makes intent explicit and reduces accidental overwrites.
| 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> |
# 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
git checkoutOtherwise, standardize on git switch and git restore.
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