git clean
Remove untracked files from the working directory
Practical example
git clean -n # preview what will be removed git clean -fd # remove untracked files and directories git clean -fdx # remove everything not tracked
Command overview & how it works
Deletes files that are not tracked by Git — typically build artifacts, logs, or generated files that are not in your .gitignore. This is a destructive command, so always preview first with git clean -n (or --dry-run), which shows what would be deleted without actually removing anything. Use git clean -f to force removal of files, git clean -fd to also remove untracked directories, and git clean -fx to remove ignored files (those matching .gitignore) as well. For maximum cleaning, git clean -fdx removes untracked files, directories, and ignored files — useful for wiping a build directory before a fresh build. Combine with git reset --hard for a complete reset to the last committed state.
Disaster-proof your Git workflow with the Field Guide
Learn how to rescue deleted branches, revive detached HEADs, and undo bad rebases without panic. 97-page PDF + ePub — ₹499.
Practical tips
- Always preview with git clean -n before deleting anything — git clean cannot be undone.
- Use git clean -fdx to wipe untracked files, directories, and ignored files before a fresh build.
- Combine with git reset --hard for a complete return to the last committed state.
Deep-dive guides & tutorials
In-depth walkthroughs and practical scenarios related to git clean
- Tutorial
How to Undo Almost Anything in Git
From amending commits to recovering deleted branches — a practical guide to Git's safety net commands.
7 min readRead guide - Tutorial
Git Reflog: Recover Lost Commits and Branches
Deleted a branch? Lost commits after a reset? Git reflog tracks every movement of HEAD and can recover what seems lost forever.
5 min readRead guide - Tutorial
Git Stash: Save and Restore Work in Progress
Stop committing half-finished work. Learn how git stash temporarily shelves changes so you can switch contexts cleanly.
5 min readRead guide
Related commands
More from Stashing & Cleaning