Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git restore: restore files easily

git restore is the modern command to undo changes, unstage files or restore from a commit. Clear, simple and unambiguous.

Modern

What is git restore?

git restore manipulates files in the working directory and staging area. It's the modern command that replaces git checkout -- file and git reset HEAD file.

Two main modes: without a flag, it restores the working directory. With --staged, it unstages files.

Git restore syntax

git restore file

Undo changes to the file

git restore --staged file

Remove the file from staging

git restore --source HEAD~1 file

Restore from a specific commit

Git restore in practice

Undo changes or unstage files.

Restore files

Unstage files

Essential restore commands

git restore file

Undo unstaged changes

git restore --staged file

Remove from staging (unstage)

git restore .

Restore all modified files

git restore --source HEAD~2 .

Restore from 2 commits ago

git restore --staged --worktree f

Unstage and restore at once

git restore --source main f

Restore from another branch

Common mistakes with git restore

Restoring without checking the diff

restore overwrites your changes without confirmation. Use git diff file before to check what you will lose.

Confusing restore and restore --staged

Without a flag, restore overwrites changes. With --staged, it just removes from staging (changes remain). This is a crucial difference.

Thinking restore is reversible

Uncommitted changes overwritten by restore are permanently lost. There is no Ctrl+Z for uncommitted changes.

Part of the Undo Changes in Git guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git restore

Master file restoration with GitQuest

Learn to manipulate your files and staging area with confidence.

Start the investigations