Home·Terminal commands

git restore

git

Usage

git restore [--staged] [-s <commit>] <pathspec>

The git 2.23 command for putting file contents back; by default it overwrites your working file and the uncommitted edits are gone, while --staged only unstages.

Common flags

FlagMeaning
--stagedunstage the file but keep your edits
--worktreereset the file on disk; this is the default
--staged --worktreeboth: throw the edits away and unstage
-s <commit>take the content from that commit instead of the index
-pchoose hunk by hunk

Examples

git restore src/app.ts

wipes uncommitted edits to that file, with no way back

git restore --staged src/app.ts

takes it out of the staging area, edits intact

git restore -s HEAD~2 src/app.ts

brings back the version from two commits ago

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git restore do?

The git 2.23 command for putting file contents back; by default it overwrites your working file and the uncommitted edits are gone, while --staged only unstages.

Q. How do I type it?

git restore [--staged] [-s <commit>] <pathspec> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-restore