bashbeginner

Git Reset and Restore File Changes

Commands to discard, unstage, or restore file changes using git restore and git checkout.

bash
# 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 -fdn

Use 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.