Skip to content

restore Git Command Guide

The git restore command restores specified paths in the working tree with contents from a restore source. It can restore files from the index, HEAD, or any commit, and can operate on the working tree, index (staging area), or both.

Terminal window
git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>...
git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>...]
OptionDescription
--stagedRestore from index to HEAD (unstages changes)
--worktreeRestore from index to working tree (default)
--staged --worktreeRestore both index and working tree
OptionDescription
--source=<tree>Restore from specified commit/tree (default: HEAD for —staged, index for —worktree)
-s <tree>Short form of —source
OptionDescription
--patch, -pInteractively select hunks to restore
--pathspec-from-file=<file>Read pathspec from file
--pathspec-file-nulUse NUL delimiter for pathspec file
-q, --quietSuppress output
--progressShow progress
--no-progressHide progress
ParameterDescription
<pathspec>Files/directories to restore
<tree>Source commit/tree for restoration
Command Comparison:
├── git restore --worktree: Restore working tree (like git checkout <commit> <file>)
├── git restore --staged: Unstage changes (like git reset HEAD <file>)
├── git restore --staged --worktree: Reset file completely (like git checkout HEAD <file>)
├── git checkout <file>: Older way to restore working tree
├── git reset HEAD <file>: Older way to unstage
Available Sources:
├── Index: Default for --worktree operations
├── HEAD: Default for --staged operations
├── Any commit: Specified with --source=<commit>
├── Branch names: --source=main
├── Relative refs: --source=HEAD~1
└── Tree objects: Direct tree SHA-1
Restore Safety:
├── Never affects branch history
├── Only modifies working tree or index
├── Preserves untracked files
├── Can be undone with git checkout
└── No risk of detached HEAD state
Terminal window
# Restore file to match index (discard working tree changes)
git restore file.txt
# Restore multiple files
git restore file1.txt file2.txt
# Restore directory
git restore src/
# Restore with pattern
git restore "*.tmp"
Terminal window
# Unstage file (restore index to HEAD)
git restore --staged file.txt
# Unstage multiple files
git restore --staged file1.txt file2.txt
# Unstage all changes
git restore --staged .
# Unstage specific directory
git restore --staged src/
Terminal window
# Reset file completely (index + working tree to HEAD)
git restore --staged --worktree file.txt
# Reset multiple files
git restore --staged --worktree file1.txt file2.txt
# Reset entire directory
git restore --staged --worktree src/
Terminal window
# Restore from specific commit
git restore --source=HEAD~1 file.txt
# Restore from different branch
git restore --source=main file.txt
# Restore from tag
git restore --source=v1.0 file.txt
# Restore from specific commit
git restore --source=abc123 file.txt
Terminal window
# Interactively restore hunks
git restore --patch file.txt
# Interactively unstage hunks
git restore --patch --staged file.txt
# Choose hunks to restore
# y: restore this hunk
# n: do not restore this hunk
# s: split into smaller hunks
# q: quit
Terminal window
# Restore from file list
git restore --pathspec-from-file=files-to-restore.txt
# Restore with NUL delimiters
git restore --pathspec-from-file=files.txt --pathspec-file-nul
# Combine with find
find . -name "*.tmp" -print0 | xargs -0 git restore
Terminal window
# Restore conflicted file to pre-merge state
git restore --source=HEAD file.txt # Restore to our side
git restore --source=MERGE_HEAD file.txt # Restore to their side
# Reset merge conflict markers
git restore --staged --worktree file.txt # Reset to HEAD
# Clean up after conflict resolution
git restore --staged file.txt # Unstage resolved file
Terminal window
# Configure restore behavior
git config restore.default untracked # Handle untracked files
git config restore.default ignored # Handle ignored files
# Configure progress display
git config restore.progress true
git config restore.quiet false
Terminal window
# Check what will be restored
git status
git diff file.txt # See working tree changes
git diff --staged file.txt # See staged changes
# Backup before complex restores
cp file.txt file.txt.backup
# Verify restore result
git status
git diff file.txt
Terminal window
# Choose restore type based on situation:
# - git restore file.txt: Discard working tree changes
# - git restore --staged file.txt: Unstage changes
# - git restore --staged --worktree file.txt: Complete reset
# - git restore --source=<commit> file.txt: Restore from specific version
# General rule: Use --staged for unstaging, --worktree for discarding changes
#!/bin/bash
# Safe file state management with restore
manage_file_state() {
local file="$1"
local action="$2"
case "$action" in
"backup")
# Create backup before changes
cp "$file" "${file}.backup"
echo "Backup created: ${file}.backup"
;;
"restore")
# Restore from backup
if [ -f "${file}.backup" ]; then
git restore "$file"
mv "${file}.backup" "$file"
echo "File restored from backup"
else
echo "No backup found"
fi
;;
"clean")
# Clean up backup
rm -f "${file}.backup"
echo "Backup cleaned up"
;;
esac
}
manage_file_state "important.txt" "backup"
# Make changes...
manage_file_state "important.txt" "restore"
Terminal window
# Integrate restore into development workflow
development_restore() {
local branch="${1:-main}"
echo "Restoring development environment..."
# Stash current work
git stash push -m "Pre-restore stash"
# Restore to clean branch state
git restore --staged --worktree .
# Switch to target branch
git checkout "$branch"
# Restore working directory
git restore .
echo "Development environment restored"
}
development_restore "feature-branch"
Terminal window
# Automated file cleanup with restore
automated_cleanup() {
echo "Performing automated cleanup..."
# Restore config files to repository versions
git restore --staged --worktree "*.config" "*.ini" "*.json"
# Unstage generated files
find . -name "*.generated" -exec git restore --staged {} \;
# Clean up temporary files
find . -name "*.tmp" -exec git restore {} \;
echo "Automated cleanup complete"
}
automated_cleanup
Terminal window
# Check what restore will do
git status
git diff file.txt
git diff --staged file.txt
# Preview restore impact
git restore --dry-run file.txt # If supported
# Recover from mistaken restore
git checkout HEAD file.txt # Restore from HEAD
git checkout @ file.txt # Alternative syntax
Terminal window
# Handle restore during merge conflicts
resolve_restore_conflicts() {
echo "Resolving restore conflicts..."
# Check conflict state
git status
# Choose restore source
git restore --source=HEAD file.txt # Restore to our version
git restore --source=MERGE_HEAD file.txt # Restore to their version
# Mark as resolved
git add file.txt
}
resolve_restore_conflicts
Terminal window
# Handle permission issues during restore
fix_restore_permissions() {
echo "Fixing restore permission issues..."
# Check file permissions
ls -la file.txt
# Fix ownership
sudo chown $(whoami) file.txt
# Fix permissions
chmod 644 file.txt
# Retry restore
git restore file.txt
}
fix_restore_permissions
Terminal window
# Handle large files efficiently
restore_large_files() {
echo "Restoring large files..."
# Check file sizes
find . -type f -size +100M -exec ls -lh {} \;
# Restore large files individually
find . -type f -size +100M -exec git restore {} \;
# Monitor progress
echo "Large file restore complete"
}
restore_large_files
Terminal window
# Prepare files for code review
prepare_for_review() {
echo "Preparing files for code review..."
# Unstage all changes
git restore --staged .
# Restore working tree to HEAD
git restore .
# Clean up untracked files
git clean -fd
# Verify clean state
git status
echo "Files prepared for review"
}
prepare_for_review
Terminal window
# Manage experimental changes safely
experimental_changes() {
local file="$1"
echo "Managing experimental changes for $file..."
# Create backup
cp "$file" "${file}.experimental"
# Make experimental changes
# ... edit file ...
# Test changes
if run_tests; then
echo "✓ Experimental changes successful"
# Keep changes
else
echo "✗ Experimental changes failed"
# Restore original
git restore "$file"
echo "Original file restored"
fi
# Clean up backup
rm -f "${file}.experimental"
}
experimental_changes "config.txt"
Terminal window
# Prepare selective commits with restore
selective_commit_prep() {
echo "Preparing selective commit..."
# Stage all changes
git add .
# Interactively unstage unwanted changes
git restore --patch --staged
# Review staged changes
git diff --staged
# Commit selected changes
git commit -m "Selective commit"
echo "Selective commit prepared"
}
selective_commit_prep
Terminal window
# Clean up repository state
repository_cleanup() {
echo "Cleaning up repository state..."
# Restore tracked files to HEAD
git restore --staged --worktree .
# Remove untracked files
git clean -fd
# Reset submodules if any
git submodule foreach git restore --staged --worktree .
# Verify clean state
if [ -z "$(git status --porcelain)" ]; then
echo "✓ Repository state cleaned"
else
echo "⚠ Some files still modified"
git status
fi
}
repository_cleanup
Terminal window
# Backup and recovery with restore
backup_recovery_workflow() {
local action="$1"
local file="$2"
case "$action" in
"backup")
# Create backup before risky operations
git restore --source=HEAD "$file"
cp "$file" "${file}.backup"
echo "Backup created for $file"
;;
"restore")
# Restore from backup
if [ -f "${file}.backup" ]; then
cp "${file}.backup" "$file"
git add "$file"
echo "File restored from backup"
else
echo "No backup found"
fi
;;
"cleanup")
# Clean up backups
find . -name "*.backup" -delete
echo "Backups cleaned up"
;;
esac
}
backup_recovery_workflow "backup" "important.txt"
# ... do risky operations ...
backup_recovery_workflow "restore" "important.txt"
backup_recovery_workflow "cleanup"

What’s the difference between restore and checkout?

Section titled “What’s the difference between restore and checkout?”

git restore is modern command for file operations; git checkout can also restore files but has multiple purposes and can accidentally create detached HEAD.

git restore —staged unstages the file, equivalent to git reset HEAD .

Can I restore a file from a specific commit?

Section titled “Can I restore a file from a specific commit?”

Yes, git restore —source= restores the file to the state from that commit.

What’s the difference between restore and reset?

Section titled “What’s the difference between restore and reset?”

restore operates on files without changing branch history; reset changes branch pointers and can affect multiple commits.

git restore discards working tree changes for that file, restoring it to match the index.

Yes, git restore file1.txt file2.txt or git restore *.txt for pattern matching.

git restore will restore the file from the index if it was tracked.

Always check git status first, use —staged for unstaging, and create backups for important files.

Yes, git restore restores all files in that directory recursively.

Check git status and git diff to see current changes before using restore.

Yes, git restore —source=origin/main restores from remote branch (must be fetched first).

What’s the difference between restore and revert?

Section titled “What’s the difference between restore and revert?”

restore affects individual files; revert creates new commits that undo changes to multiple files.

How do I restore a file to a previous version?

Section titled “How do I restore a file to a previous version?”

git restore —source= restores the file to that commit’s version.

Yes, restore works with all file types, including binary files.

How do I restore all files in working tree?

Section titled “How do I restore all files in working tree?”

git restore . restores all files in current directory recursively.

Yes, restore is script-friendly with predictable behavior and clear exit codes.

  1. File State Management: Safely restore files to previous states without affecting branch history
  2. Staging Area Control: Unstage files and changes selectively before committing
  3. Working Tree Cleanup: Discard unwanted changes and restore clean file states
  4. Commit Preparation: Prepare selective commits by unstaging unwanted changes
  5. Conflict Resolution: Restore files during merge conflict resolution
  6. Development Workflow: Manage experimental changes and file backups safely