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.
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 fileUndo changes to the file
git restore --staged fileRemove the file from staging
git restore --source HEAD~1 fileRestore from a specific commit
Git restore in practice
Undo changes or unstage files.
Restore files
Unstage files
Essential restore commands
git restore fileUndo unstaged changes
git restore --staged fileRemove from staging (unstage)
git restore .Restore all modified files
git restore --source HEAD~2 .Restore from 2 commits ago
git restore --staged --worktree fUnstage and restore at once
git restore --source main fRestore 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
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