↩️ UndoingCommonly used
How to discard uncommitted changes in Git
Remove unstaged or staged changes from your working directory.
Discard changes in a specific file
git restore filename.txtReverts the file to its state at the last commit. Modern syntax (Git 2.23+).
Permanently discards unsaved changes in the file.
Discard changes in a file (older syntax)
git checkout -- filename.txtClassic way to discard changes in a file.
Permanently discards unsaved changes.
Discard all unstaged changes
git restore .Reverts all modified tracked files to their last committed state.
Permanently discards all unstaged changes across the entire project.
Remove all untracked files
git clean -fdDeletes all untracked files (-f) and directories (-d).
Permanently deletes untracked files. Use git clean -n first to preview what will be deleted.
Preview what clean would delete
git clean -nDry run — shows what would be deleted without actually deleting anything.
When to use: Always run this before git clean -fd.