Cheat Sheets · Git Cheat Sheet · Git Clean Untracked Files — Remove Clutter Safely
Git Clean Untracked Files — Remove Clutter Safely
git clean -nSafe (Dry run)
git clean -fdDestructive
git clean -fdxVery Destructive
Git clean commands
| Command | Safety | Effect |
|---|---|---|
git clean -n | Safe (Dry run) | List files that would be removed without deleting anything |
git clean -fd | Destructive | Force delete untracked files and directories |
git clean -fdx | Very Destructive | Also delete ignored files (e.g. node_modules, .env, build/) |
Clean up build artifacts, temp files, and untracked junk from your working tree. Always start with a dry run to avoid deleting wanted files.
Related Git recipes
Undo Last Commit — 3 Safe Ways
Discard Local Changes — Safely and Completely
Delete a Branch — Local and Remote
Stash — Park Changes Without Committing
Amend the Last Commit
Undo a Pushed Commit — The Safe Way (git revert)
Rename Local Branch — Safely & Quickly
Rename Remote Branch — Complete 3-Step Process
Cherry Pick — Apply a Specific Commit Elsewhere
Squash Last N Commits — Combine Multiple Commits
Sync Fork with Upstream — Step-by-Step
Delete Remote Tag — Local and Remote
Change Commit Author — Email & Name
Unstage File — Remove from Staging Area
Log Graph — Visual Commit History in Terminal
Switch Branch — Modern Branch Navigation
Create Branch from Commit — Branch Off History
Stash Untracked Files (-u and -a Flags)
Force Pull — Overwrite Local Changes with Remote
Resolve Merge Conflict — Complete Workflow
Show Commit — Inspect Changes in a Commit
Tag a Release — Lightweight & Annotated Tags
FAQ
Can I recover files deleted by git clean?
No! Because untracked files were never committed to Git, Git has no copy in its object database. Always dry-run with -n first.
What does the -d flag mean?
It instructs clean to remove untracked whole directories in addition to individual files.
How to clean only a specific subdirectory?
Pass the path: git clean -fd ./dist.
Stop memorizing — build it interactively
Try clean commands in the Git BuilderFull reference: Git Cheat Sheet