restore command enables developers to restore files to a specific state. This functionality restoring to undo any changes made to files in both the working directory and staging area. This command is particularly valuable when reverting alterations, whether staged for a commit or not.
The syntax of the
git restore command is as follows:
git restore <options> -- <file>
Nine additional options are available for
-s <tree> --source=<tree>: Allows restoring the working tree files with the content from the specified tree.
-p --patch: Enables interactive selection of hunks in the difference between the restore source and the restore location.
-W --worktree -S --staged: Specifies the restore location. If neither option is specified, by default the working tree is restored by default.
-q --quiet: Suppresses feedback messages and implies
--progress --no-progress: Control progress status reporting.
--ours --theirs: Used when restoring files in the working tree from the index, to use stage #2 (ours) or #3 (theirs) for unmerged paths.
-m --merge: Recreates the conflicted merge in the unmerged paths when restoring files to the working tree from the index.
--conflict=<style>: Modifies the presentation of conflicting hunks, overriding the
--ignore-unmerged: Allows restoring files to the working tree from the index without aborting due to unmerged entries.
In this example, the initial
git status command shows that there are changes in the
index.html file that have not been staged for a commit. It suggests using
git add to stage these changes. However, the user decides to use
git restore index.html to discard the changes in the working directory and revert the file to its last committed state. After running this command and checking
git status again, the working directory is now clean, indicating that there are no pending changes, and there’s nothing left to commit.
$ git statusOn branch mainChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")$ git restore index.html$ git statusOn branch mainnothing to commit, working tree clean