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
| Command | What 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.
Perguntas frequentes
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
Git Config Essentials
Set identity, defaults, and aliases for Git on a new machine.
Git Clone and Init
Start a repo from scratch or copy a remote repository locally.
Git Add and Commit
Stage changes and record them as commits with clear messages.
Git Branch Basics
Create, list, rename, and delete branches for parallel work.