Git Restore
# git restore Command
[Git Basic Operations](#)
* * *
**git restore** command is used to restore or revert file changes.
The `git restore` command was introduced in Git version 2.23 to simplify and improve file restoration operations. Compared to older commands (such as `git checkout` and `git reset`), it focuses more on restoring file content and working directory state.
`git restore` can restore files in both the working directory and staging area, and can also be used to discard uncommitted changes.
### Basic Syntax
git restore [] [...]
* **``**: The file or directory path to restore.
* **``**: Options to customize the restore behavior.
**Common Options and Usage**
| **Option** | **Description** | **Usage Example** |
| --- | --- | --- |
| `--source=` | Restore file content from the specified commit. Defaults to HEAD, which is the current commit. | `git restore --source=HEAD~1 file.txt` |
| `--staged` | Restore file content in the staging area to the working directory, rather than restoring content in the working directory. | `git restore --staged file.txt` |
| `--worktree` | Restore file content in the working directory to the current working directory state. | `git restore --worktree file.txt` |
| `--ours` | During merge conflicts, restore to the current branch's version (i.e., "our" version). | `git restore --ours file.txt` |
| `--theirs` | During merge conflicts, restore to another branch's version (i.e., "their" version). | `git restore --theirs file.txt` |
| `--conflict=` | Specify the merge conflict style, such as `merge` or `diff3`. | `git restore --conflict=diff3 file.txt` |
| `--dry-run` | Display the files and paths that will be restored, without actually performing the restoration. | `git restore --dry-run` |
| `-s`, `--source` | Same as `--source`, used to restore files from a specified commit. | `git restore -s HEAD~1 file.txt` |
| `-W`, `--worktree` | Restore file content in the working directory to the current working directory state (same as `--worktree`). | `git restore -W file.txt` |
| `-S`, `--staged` | Restore file content in the staging area to the working directory, rather than restoring content in the working directory (same as `--staged`). | `git restore -S file.txt` |
* * *
## Examples
### Restore Files in Working Directory
Restore files in the working directory to the state of the most recent commit (i.e., discard all uncommitted changes to the file):
git restore file.txt
### Restore Files in Staging Area
Restore files in the staging area to the working directory, which effectively removes the file from the staging area (without committing):
git restore --staged file.txt
### Restore Files from Specified Commit
Restore a file from a specific commit (e.g., HEAD~1):
git restore --source=HEAD~1 file.txt
### Restore "Our" Version of File
During merge conflicts, restore to the current branch's version (i.e., "our" version):
git restore --ours file.txt
### Restore "Their" Version of File
During merge conflicts, restore to another branch's version (i.e., "their" version):
git restore --theirs file.txt
### Display Files and Paths to Be Restored
Display the files and paths that will be restored, without actually performing the restoration:
git restore --dry-run
### Restore Multiple Files
Restore multiple files in the working directory:
git restore file1.txt file2.txt
* * Git Basic Operations](#)
YouTip