git clean
Remove untracked files from the working tree
The 'git clean' command deletes untracked files — files Git has never stored, such as logs, build output or experiments. Because Git has no copy of them, deleted files cannot be recovered. For that reason git clean refuses to run without -f, and git clean -n shows what would be deleted first. Files in .gitignore are kept unless you add -x.
Synopsis
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]Options
Choosing the Right Option
What each command removes
Tracked vs untracked
| Command | Untracked files | Ignored files | Edits to tracked files |
|---|---|---|---|
git clean -f | Deleted | Kept | Kept |
git clean -f -x | Deleted | Deleted | Kept |
git restore . | Kept | Kept | Discarded |
git stash -u | Stashed | Kept | Stashed |
Examples
Preview
git clean -nLists the untracked files that would be deleted.
Delete untracked files
git clean -fDeletes them for good.
Folders too
git clean -fdAlso deletes untracked folders.
Workflow Examples
git status -sSee the untracked files (??)
git clean -nPreview the deletion
git clean -fDelete them
Troubleshooting
fatal: clean.requireForce is true and -f not given
Cause: git clean refuses to delete without -f
Run git clean -n to preview, then git clean -f
An untracked folder was not deleted
Cause: Folders need -d
Use git clean -fd
I deleted a file I needed
Cause: Untracked files have no copy in Git
It cannot be restored by Git; check your editor or backups. Next time use git stash -u
Tips & Pitfalls
Best Practices
- ✓Always run git clean -n before git clean -f.
- ✓Use git stash -u instead when you might need the files later.
- ✓Keep -x for build folders you can regenerate.
Common Mistakes
- ✗Running git clean -f without -n first and losing a file you still needed.
- ✗Adding -x by habit and deleting ignored files like .env.
- ✗Expecting git clean to undo edits to tracked files — use git restore for that.
FAQ
No. The files were never stored in Git.
No. It only deletes untracked (and with -x, ignored) files.
Yes: git clean -i lets you choose files one by one.