git restore command

Git Basic Operations


git restore The command is used to restore or undo changes to files.

git restoreThe command was introduced in Git version 2.23 to simplify and improve file restoration operations. Compared to older commands (such asgit checkoutandgit reset), it focuses more on restoring file contents and the state of the working tree.

git restoreIt can restore files in the working tree and the staging area, and can also be used to discard uncommitted changes.

Basic Syntax

git restore [<options>] [<pathspec>...]
  • <pathspec>: The path of the file or directory to restore.
  • <options>: Options for customizing restore behavior.

Common options and usage

OptionsDescriptionUsageExample
--source=<commit>Restore file contents from a specified commit. Defaults to HEAD, i.e., the current commit.git restore --source=HEAD~1 file.txt
--stagedRestore file contents from the staging area to the working tree, rather than restoring the contents in the working tree.git restore --staged file.txt
--worktreeRestore the file contents in the working tree to the current working tree state.git restore --worktree file.txt
--oursIn the event of a merge conflict, restore to the version of the current branch (i.e., the "ours" version).git restore --ours file.txt
--theirsIn the event of a merge conflict, restore to the version of the other branch (i.e., the "theirs" version).git restore --theirs file.txt
--conflict=<style>Specify the style of merge conflicts, e.g.mergeordiff3。git restore --conflict=diff3 file.txt
--dry-runShow the files and paths that would be restored, without actually performing the restore.git restore --dry-run
-s, --sourceand--sourceSame, used to restore files from a specified commit.git restore -s HEAD~1 file.txt
-W, --worktreeRestore the file contents in the working tree to the current working tree state (with--worktreesame).git restore -W file.txt
-S, --stagedRestore file contents from the staging area to the working tree, rather than restoring the contents in the working tree (with--stagedsame).git restore -S file.txt

Example

Restore files in the working tree

Restore files in the working tree to the most recent commit state (i.e., discard all uncommitted changes to the files):

git restore file.txt

Restore files in the staging area

Restore the files in the staging area to the working tree, which is effectively removing the files from the staging area (without committing):

git restore --staged file.txt

Restore files from a specified commit

Restore files from a specific commit (e.g., HEAD~1):

git restore --source=HEAD~1 file.txt

Restore the "ours" version of a file

In the event of a merge conflict, restore to the version of the current branch (i.e., the "ours" version):

git restore --ours file.txt

Restore the "theirs" version of a file

In the event of a merge conflict, restore to the version of the other branch (i.e., the "theirs" version):

git restore --theirs file.txt

Show the files and paths to be restored

Show the files and paths that would be restored, without actually performing the restore:

git restore --dry-run

Recover multiple files

Restore multiple files in the working tree:

git restore file1.txt file2.txt


Git Basic Operations

other extensions