C.W.K.
Stream
Lesson 01 of 05 · published

restore: Working Tree and Staging Undo

~18 min · restore, undo

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

restore is the precise undo for files

For years, "undo this file" in Git meant git checkout -- file, which was overloaded with branch switching and routinely confused beginners. Git 2.23 introduced git restore — a command whose only job is moving file content back across the three areas. Once you start using it, the old checkout-for-files invocations feel like accidents waiting to happen.

Three undo flavors cover almost everything. Discard unstaged edits: git restore file.js overwrites the working-tree version with the index version, throwing away the local edits. Unstage: git restore --staged file.js overwrites the index entry with the HEAD version, removing it from the next commit but keeping working-tree edits. Both at once: git restore --staged --worktree file.js rewinds both layers to the HEAD version, fully erasing recent work on the file.

The optional --source flag lets you restore from a different commit — restore --source=HEAD~3 -- file.js brings the version of file.js from three commits ago into the working tree (and optionally index). This is how you "go back to how it was last week" for one file without touching the rest of the project.

Treat restore as the quiet undo. It does not move branch pointers; it does not rewrite history; it does not affect other files. The blast radius is exactly the paths you list. That makes it safe to use freely — far safer than git reset --hard, which is often the wrong choice for a single-file mistake.

Code

Three flavors of restore·bash
# Discard unstaged edits to one file:
git restore src/app.js

# Unstage a file (keep edits, remove from next commit):
git restore --staged src/app.js

# Rewind both index and working tree to HEAD for this file:
git restore --staged --worktree src/app.js

# Restore from an older commit:
git restore --source=HEAD~3 -- src/app.js
Interactive restore is hunk-aware·bash
# Walk hunks and pick which to discard:
git restore -p src/app.js

# Walk hunks and pick which to unstage:
git restore --staged -p src/app.js

# Restore everything that has unstaged changes (DESTRUCTIVE):
git restore .

External links

Exercise

Edit one file in a project to introduce a clearly bad change. Run git restore -p <file> and discard only the bad hunk while keeping any other in-progress edits. In another scenario, stage a change with git add, decide it does not belong, run git restore --staged <file> to unstage while keeping the working-tree edit. Verify each result with git status and git diff.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.