Git Switch and Checkout

Move between branches and restore files without losing work.

  • git switch
  • git restore
  • git checkout file
  • switch branch
  • discard changes

Git Switch and Checkout

Move between branches and restore files without losing work.

Commands

CommandWhat it does
git switch <branch>Switch to an existing branch
git switch -Jump back to the previous branch
git restore <file>Discard unstaged changes in a file
git restore --source HEAD <file>Restore a file from HEAD
git checkout <commit> -- <file>Classic way to restore one file from a commit

Example

git switch main
git switch -
git restore --source HEAD config/app.yml

FAQ

I have uncommitted changes and need to switch branches — what now?

Stash them (git stash push), commit, or use git switch --merge; never force-switch with dirty files you care about.

Does switching branches delete my work?

No — tracked uncommitted changes travel with you; committed history stays on its branch.

よくある質問

I have uncommitted changes and need to switch branches — what now?
Stash them (git stash push), commit, or use git switch --merge; never force-switch with dirty files you care about.
Does switching branches delete my work?
No — tracked uncommitted changes travel with you; committed history stays on its branch.

Git