git checkout vs git switch vs git restore — what's the difference?
Git 2.23 (2019) introduced git switch and git restore to split git checkout into two focused commands. All three still work today, but the newer commands are clearer and harder to misuse.
# git checkout does THREE different things (confusing):
git checkout feature/login # switches branch
git checkout -b new-branch # creates + switches branch
git checkout -- src/app.ts # discards file changes (DANGEROUS, easy to mistype)
# git switch — branch operations only (clear intent):
git switch feature/login # switches branch
git switch -c new-branch # creates + switches branch
# git restore — file operations only (clear intent):
git restore src/app.ts # discards file changes in working dir
git restore --staged src/app.ts # unstages a file (keeps changes in working dir)
git restore --source HEAD~2 src/app.ts # restore file from 2 commits agoUse git switch and git restore for new work — they make intent explicit and prevent the accidental file-destruction bug of git checkout -- file. The old git checkout still works and won't be removed.
