Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Staging and Committing

git restore command

Reference for git restore: discard working tree changes, unstage with --staged, restore files from any commit with --source, use -p, and avoid data loss.

git restore puts file contents back. It can overwrite files in your working tree with the version from the index or from any commit, and it can reset entries in the index so that changes become unstaged. It was added in Git 2.23 to take over the "restore files" jobs that git checkout -- <file> and git reset <file> used to share with other meanings.

Two targets exist and you choose them with flags. By default the target is the working tree (--worktree). With --staged it is the index. With both, the file is restored in both places. The source is the index for the working tree and HEAD for the index, unless you give --source.

Terminal
git restore [--source=<rev>] [--staged] [--worktree] [-p] [--] <pathspec>...

Try it

Bash Discard edits and unstage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Restoring from other commits and patch mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git restore <pathspec>...
git restore --staged <pathspec>...
git restore --source=<tree-ish> [--staged] [--worktree] <pathspec>...
Option Description
<pathspec>... What to restore (at least one is required). . means everything below the current directory.
-s <tree-ish>, --source=<tree-ish> Take contents from this commit or tree instead of the default source.
-S, --staged Restore the index.
-W, --worktree Restore the working tree (default when --staged is absent).
-p, --patch Choose hunks interactively.
-q, --quiet No messages.
--ours, --theirs During a merge conflict, take that side's version for unmerged paths.
-m, --merge Recreate the conflicted merge in the file.
--conflict=<style> merge, diff3 or zdiff3 markers for -m.
--overlay, --no-overlay With overlay, files missing from the source are kept; the default --no-overlay deletes tracked files that the source does not contain.
--ignore-unmerged Skip conflicted paths instead of failing.
--recurse-submodules Also update submodule working trees.
--pathspec-from-file=<file>, --pathspec-file-nul Read pathspecs from a file.

Defaults. Without --source: the working tree is restored from the index; with --staged, the index is restored from HEAD; with --source, even --worktree alone uses that tree.

Errors: error: pathspec 'x' did not match any file(s) known to git for an unknown path; --staged --patch and similar combinations that make no sense are rejected; unmerged paths need --ours, --theirs, --merge or --ignore-unmerged.

Usage notes

  • Discarding is permanent. Restoring the working tree overwrites uncommitted edits with no backup and no reflog entry. If you may want them back, run git stash first, or copy the file.
  • Unstaging is safe. --staged only changes the index, your edits remain in the file.
  • Whole tree. git restore . restores every modified tracked file under the current directory; untracked files are untouched (use git clean for them).
  • Deleted files reappear, since the index knows them. Files in the index but missing from the chosen --source are removed (the no-overlay default) - this matters when restoring a directory from an old commit.
  • Versus old commands. git restore --staged f replaces git reset HEAD f; git restore f replaces git checkout -- f. Those still work, but restore cannot move branches or HEAD, so it is harder to misuse.
  • Initial commit. In a repository without commits, --staged removes entries from the index (nothing to restore from).
  • Does not switch branches. To change branches use git switch; to move a branch pointer use git reset.
  • Experimental when added. Git 2.23 labeled restore and switch experimental; the behavior has been stable since.