Video in progress

Junior 🌿 Git

When and why did `git switch` and `git restore` appear instead of `git checkout`?

In Git 2.23 the two different jobs of checkout were separated: switch changes branches, restore restores files. Because one command did two things, it was easy to make mistakes.

📖 Answer

The old checkout both moved to a branch and threw away changes in a file:

git checkout main          # move to a branch
git checkout -- App.java   # discard changes in the file (cannot be undone!)

The same name, completely different consequences. The new commands make this explicit:

git switch main
git switch -c feature/report      # create a new branch and move to it
git restore App.java              # discard changes on disk
git restore --staged App.java     # only remove it from staging

checkout still works and will not be removed, and many articles use it. But if you are just starting out, you are less likely to mix things up with switch/restore.