recovery
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.
git restore [<options>] <pathspec>... Common flags
| Flag | Purpose |
|---|---|
| --staged | Unstage a file (restore index from HEAD) |
| --source=<commit> | Restore file contents from a specific commit |
| --worktree | Restore in the working tree (default) |
| -p / --patch | Interactively pick hunks to restore |
Examples
git restore src/app.js Discard unstaged changes in a file
git restore --staged src/app.js Unstage a file without touching the working copy
git restore --source=HEAD~2 README.md Restore README to its version two commits ago
Gotcha
Without --staged, 'restore' overwrites working-tree changes irreversibly — there's no trash bin, so double-check paths.
Related commands
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.
git reset
Moves the current branch tip to a specified commit, optionally updating the index and working tree. Used to unstage files or rewind local history.
git add
Stages file changes into the index so they are included in the next commit. Supports whole files, directories, and interactive patch selection.
git revert
Creates a new commit that inverts the changes of a previous commit, preserving history. The safe way to undo pushed commits on shared branches.
git stash
Shelves uncommitted changes onto a stack so you can switch context with a clean working tree. Later, reapply them with pop or apply.