git restore
git restore: restore the working tree and/or staging area
git restore puts files back to a known state. By default it replaces working tree files with what is in the staging area. If nothing is staged, the result is the same as your latest commit.
With the --staged option, it resets the staging area to match the latest commit (or the commit set in --source) and leaves your working tree as-is. This is how you unstage back to the commit state without losing your edits.
Added in Git 2.23.0, git restore is the focused way to undo local changes or unstage files. If you're used to doing everything with git reset, you can keep that habit—but git restore is harder to misuse and only touches the working tree and staging area, not commit history.
Use cases
-
Restore working tree (nothing staged): You made changes you do not want. Put the working tree back to the latest commit.
Solution:
git restore --worktree .OR simplygit restore ., because the--worktreemode is default. -
Restore staging area, keep working tree: You staged too much. Put the staging area back to the latest commit, keep your working tree edits.
Solution:
git restore --staged . -
Restore working tree, keep staging area: You staged the right stuff, then added more edits you dislike. Put the working tree back to what is staged.
Solution:
git restore --worktree .ORgit restore . -
Restore staging area and working tree: You want to throw everything away—staged and unstaged. Put both back to the latest commit.
Solution:
git restore --worktree --staged .
Examples
Discard changes in a specific file:
git restore README.mdDiscard changes in every file in the current directory (the . path means "here"):
git restore .Unstage a file but keep its edits in the working tree:
git restore --staged README.mdUnstage everything and reset the working tree to the latest commit:
git restore --worktree --staged .Restore a file from the specific commit.
git restore --source a1b2c3d4 README.mdPick changes to keep or discard interactively:
git restore --patchOr the shorter form:
git restore -p