Progress:
TIER 1 · MODULE 12· Basics

git restore

Modern, intent-clear replacement for half of git checkout.

🎯 What & why

Modern, intent-clear command for undoing changes in the working tree or index. Replaces the half of git checkout that handled file restoration. New in Git 2.23, recommended since.

🧠 Mental model

Restore touches paths, not refs. The --source option says where to copy from (defaults to HEAD or the index). --worktree (default) writes to the working tree. --staged writes to the index. Combine flags to do both.

🛠️ Synopsis

git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>...
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>...]

🎚️ Switches & options

FlagWhat it does
--source=<tree>Take the file content from <tree> (commit, branch, tag). Default: index for working tree, HEAD for index.
--staged, -SRestore the index. Effectively unstages.
--worktree, -WRestore the working tree (default).
-p, --patchPick hunks to restore.

💡 Use cases

🧪 Examples

Throw away unstaged changes to a file.
$ git restore src/main.c
Unstage a file (keep your edits).
$ git restore --staged src/main.c
Unstage and discard the working-tree changes.
$ git restore --staged --worktree src/main.c
Bring back a file from a different commit.
$ git restore --source=HEAD~2 -- README.md

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.