The git restore
command is used to restore files in your working directory to a previous state. It allows you to discard changes made to files or restore files that were deleted. The basic syntax of git restore
is as follows:
git restore <file>
Here are a few common use cases of git restore
:
Discard Local Changes: To discard the changes made to a specific file and revert it to the state of the last commit, you can run the following command:
git restore <file>
Replace <file>
with the name of the file you want to restore. This command discards any modifications made to the file and reverts it to the state of the last commit.
Restore Deleted Files: If you have deleted a file and want to restore it to the state of the last commit, you can use the following command:
git restore --source=HEAD <file>
This command restores the deleted file to the state of the last commit. The --source=HEAD
option specifies that the file should be restored from the last commit.
Restore Files from a Specific Commit: If you want to restore files to a specific state from a previous commit, you can provide the commit hash or reference to the git restore
command. For example:
git restore --source=<commit> <file>
Replace <commit>
with the commit hash or reference you want to restore the file from. This command restores the file to the state of the specified commit.
It’s important to note that git restore
modifies the files in your working directory, but it does not create a new commit. If you want to create a new commit to record the restored changes, you need to use git add
and git commit
after using git restore
.
Additionally, the behavior of git restore
can be affected by various options and flags, such as --staged
to restore changes to the staging area, or --worktree
to restore changes to the working directory. You can refer to the Git documentation or use git restore --help
for more detailed information and usage examples specific to your needs.