Restore
The 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.
Syntax
The syntax of the git restore
command is as follows:
git restore <options> -- <file>
Nine additional options are available for git restore
:
-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--no-progress
.--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 themerge.conflictStyle
configuration variable.--ignore-unmerged
: Allows restoring files to the working tree from the index without aborting due to unmerged entries.
Example
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
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Git on Codecademy
- Course
Learn Git & GitHub
Use our beginner friendly Git course to integrate Git and GitHub and manage versions of your projects using Git branches.With CertificateBeginner Friendly4 hours - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours