git restore command
The **git restore** command is used to restore or undo changes to files.
git restore The command was introduced in Git 2.23 to simplify and improve file restoration operations. Compared with older commands (such as git checkout and git reset), it focuses more on restoring file content and working directory state.
git restore It can restore files in the working directory and staging area, and can also be used to discard uncommitted changes.
Basic Syntax
git restore [] [...]
: the path of the file or directory to restore.: options for customizing the restore behavior.
Common options and usage
| Option | Description | Usage Examples |
|---|---|---|
--source= |
Restore file content from a specified commit. The default is HEAD, i.e., the current commit. | git restore --source=HEAD~1 file.txt |
--staged |
Restore file content from 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 a merge conflict, restore to the current branch's version (i.e., "ours"). | git restore --ours file.txt |
--theirs |
During a merge conflict, restore to the other branch's version (i.e., "theirs"). | git restore --theirs file.txt |
--conflict= |
Git Basic Operations