Git Reset and Restore File Changes
Commands to discard, unstage, or restore file changes using git restore and git checkout.
# Discard all unstaged changes in a file
git restore src/app.ts
# Discard all unstaged changes in the entire repo
git restore .
# Unstage a file (keep changes in working tree)
git restore --staged src/app.ts
# Unstage all files
git restore --staged .
# Restore a file from a specific commit
git restore --source=abc1234 src/app.ts
# Restore a deleted file from the last commit
git restore --source=HEAD -- deleted-file.ts
# Classic alternatives (still work):
git checkout -- src/app.ts # discard changes
git reset HEAD src/app.ts # unstage file
# Discard all changes and untracked files
git checkout -- .
git clean -fd
# Review what will be cleaned
git clean -fdnUse Cases
- Discarding experimental changes in files
- Unstaging accidentally added files
- Restoring deleted files from history
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
Git Rebase Workflow Example
Step-by-step git rebase workflow to keep feature branches up to date with main branch cleanly.
Git Cherry-Pick Commits Example
Apply specific commits from one branch to another using cherry-pick with conflict resolution.
Squash Multiple Git Commits
Interactive rebase to squash multiple commits into one clean commit before merging a feature branch.