git-restore - Linux


Overview

git-restore is a Git command used to restore untracked files or delete files from the index. It is commonly employed during code development or when working with sensitive data that should not be committed to the Git repository.

Syntax

git restore [--worktree <directory>] [--staged <pathspec>...] [--untracked <pathspec>...] [--pathspec-from-file=<file>] [--prefix=<path>] [--source=<path>|HEAD]

Options/Flags

  • –worktree : Specify the working tree to use for restoration.
  • –staged : Restore staged files matching the specified pathspec.
  • –untracked : Restore untracked files matching the specified pathspec.
  • –pathspec-from-file=: Read pathspec from the specified file.
  • –prefix=: Prepend the specified prefix to all restored paths.
  • –source=|HEAD: Specify the source from which to restore. Default is HEAD.

Examples

Restore all untracked files:

git restore --untracked

Restore a specific staged file:

git restore --staged file.txt

Restore a deleted file from HEAD:

git restore --source=HEAD deleted_file.py

Restore and prefix files before untracked:

git restore --prefix=untracked/

Common Issues

File not found: Ensure the specified path exists and is correct.
Permission denied: Check file permissions and ensure you have write access.
No changes detected: Verify that the specified file has not already been restored.

Integration

Undo Git commit with git-restore:

git reset HEAD --hard && git restore

Discard local changes with git-restore:

git restore --untracked && git restore --staged

Related Commands