Restoring
Restoring is the process of reverting changes in your working directory
and/or staging area to a previous state without changing the commit history.
Restoring is useful when you want to undo changes, remove files from the
staging area, or restore a file to a previous state.
Use Cases:
- Discarding changes in the working directory
- Restoring the project folder to a previous state
- Restoring a file to a previous state
- Removing files from the staging area
Examples:
Usage of the git restore command:
git restore [options] [filename/path]
Removing a file from the staging area
git restore --staged [filename]
Discarding changes in the working directory (restoring to previous commit state)
# The dot represents all files in current directory
git restore .
git restore .
Restoring a file to a previous state
# HEAD~1 represents the commit before the last commit
git restore --source=HEAD~1 [filename]
git restore --source=HEAD~1 [filename]
Restoring files in both the working directory and staging area
git restore --staged --worktree .