Modern, intent-clear replacement for half of git checkout.
Modern, intent-clear command for undoing changes in the working tree or index. Replaces the half of git checkout that handled file restoration. New in Git 2.23, recommended since.
Restore touches paths, not refs. The --source option says where to copy from (defaults to HEAD or the index). --worktree (default) writes to the working tree. --staged writes to the index. Combine flags to do both.
git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>...
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>...]| Flag | What it does |
|---|---|
--source=<tree> | Take the file content from <tree> (commit, branch, tag). Default: index for working tree, HEAD for index. |
--staged, -S | Restore the index. Effectively unstages. |
--worktree, -W | Restore the working tree (default). |
-p, --patch | Pick hunks to restore. |
$ git restore src/main.c$ git restore --staged src/main.c$ git restore --staged --worktree src/main.c$ git restore --source=HEAD~2 -- README.mdrestore over checkout -- <path>. Same effect, clearer intent. Tooling and docs are moving this way.git status after every restore — confirm what's left.git restore <path> is destructive of unstaged changes. There is no undo (the changes were never in the index).--staged, a restore won't unstage — you'll be confused why your git status still shows staged changes.Hit each option, then Check answers. Score is recorded; Next is always open.