git checkout Switch branches or restore files (older all-in-one)
The original multi-purpose command: it switches branches, creates them, and restores files. Newer Git splits those jobs into git switch and git restore, but checkout is everywhere in guides and scripts.
Synopsis
git checkout [OPTION]... BRANCH
git checkout [COMMIT] -- PATH...
Common options
| Option or form | What it does |
|---|---|
BRANCH | Switch to that branch |
-b NAME | Create a new branch and switch to it |
-- PATH | Restore a file from the index or a commit. This discards your edits |
COMMIT | Check out a commit, which leaves you in "detached HEAD" |
- | Go back to the previous branch |
Examples
git checkout main
git checkout -b feature/search
git checkout -- app.js
git checkout main -- config.json
git checkout 3f2a9c1
Notes
- Restoring a file with checkout overwrites your edits with no undo. Check git diff first.
- Detached HEAD means new commits belong to no branch. Run git switch -c NAME to keep them.
- Prefer git switch and git restore in new work. The older command still works.
See also
Last reviewed 2026-10-09.