Terminal GuideTerminal Guide

git restore Command Guide

Restore working tree files

5 min readLast updated: January 19, 2026
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Working Dir

git restore <file>Discard changes
git restore .Discard all changes
git restore -p <file>Interactive restore

Staging

git restore --staged <file>Unstage file
git restore --staged .Unstage all

Source

--source=HEAD~1From previous commit
--source=mainFrom another branch

Downloadable Image Preview

Failed to generate preview

Basic Usage

The git restore command (Git 2.23+) restores working tree files. It's a clearer alternative to git checkout for file operations.

bash
# Discard changes in working directory
git restore file.js

# Unstage a file
git restore --staged file.js

# Restore from a specific commit
git restore --source=HEAD~1 file.js

Discarding Changes

Restore files to their last committed state:

bash
# Discard changes in a single file
git restore file.js

# Discard changes in multiple files
git restore file1.js file2.js

# Discard all changes in working directory
git restore .

# Interactive: choose which changes to discard
git restore -p file.js
Warning
git restore permanently discards uncommitted changes. Make sure you don't need those changes before running this command.

Unstaging Files

bash
# Unstage a file (keep working directory changes)
git restore --staged file.js

# Unstage all files
git restore --staged .

# Unstage specific files
git restore --staged src/app.js src/utils.js

# Interactive unstaging
git restore --staged -p file.js

Unstage vs Discard

  • --staged: Moves from staging to working dir (keeps changes)
  • (no flag): Discards working directory changes

Restoring from Source

bash
# Restore from previous commit
git restore --source=HEAD~1 file.js

# Restore from specific commit
git restore --source=abc1234 file.js

# Restore from another branch
git restore --source=main file.js

# Restore entire directory from commit
git restore --source=v1.0.0 src/

# Restore and unstage (both working dir and staging)
git restore --source=HEAD --staged --worktree file.js

Restore vs Checkout

bash
# Old way (checkout)
git checkout -- file.js          # Discard changes
git checkout HEAD file.js        # Restore from HEAD
git checkout abc1234 -- file.js  # Restore from commit

# New way (restore) - clearer intent
git restore file.js              # Discard changes
git restore --source=HEAD file.js
git restore --source=abc1234 file.js

Practical Examples

bash
# Accidentally modified a file - discard changes
git restore file.js

# Added wrong files to staging
git restore --staged *.log

# Get old version of config file
git restore --source=v1.0.0 config.json

# Undo all changes, start fresh
git restore .
git restore --staged .

# Partially discard changes
git restore -p file.js
# Choose 'y' or 'n' for each hunk

# Restore deleted file
git restore deleted-file.js

Frequently Asked Questions

What is the difference between git restore and git checkout?

git restore (Git 2.23+) is specifically for restoring files, making it clearer and safer. git checkout does both branch switching and file restoration, which can be confusing.

How do I unstage a file?

Use "git restore --staged <file>" to unstage a file while keeping the changes in your working directory.

Can I recover files after git restore?

If the changes were never committed, they cannot be recovered. git restore permanently discards uncommitted working directory changes.

How do I restore a file from another branch?

Use "git restore --source=<branch> <file>" to get the version of a file from another branch.

Summary

git restore is the modern, clear way to restore files. Use --staged to unstage, no flag to discard working directory changes.

Quick Reference

  • git restore <file> - Discard changes
  • git restore --staged <file> - Unstage
  • git restore --source=<ref> - From source
  • git restore -p - Interactive

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles