git checkout
Switches branches or restores working tree files.
Syntax
git checkout [<options>] <branch> git checkout [<options>] -- <file>...
Options
- -b <new-branch>: Creates a new branch and switches to it
- -B <new-branch>: Creates or resets a branch and switches to it
- --track: Sets up tracking for a remote branch
- -- <file>: Restores a specific file from the index (discards working changes)
Example
git checkout main git checkout -b feature/new git checkout -- src/app.ts
Tip
Prefer git switch and git restore (Git 2.23+) for clearer intent.