Git Clean (git clean)
What Will We Learn?
We’ll learn how to use git clean to remove untracked files and directories, along with safe usage practices using dry-run and interactive modes.
Clean Working Directory — git clean
Over time your workspace will be messed with untracked files, temp files, build artifacts, configs and envs that should not be versioned. They may be fine initially (envs still are not) but later it will become a mess. And it’s always good to clean up the mess. That will be done by git clean. git clean alone won’t do anything, you need to combine it with several options.
Common Use Cases
Preview what will be deleted (always do this first!)
git clean -n
-n(or--dry-run) shows what would be removed, without deleting anything.
Remove untracked files
git clean -f
-f(force) is required by default Git config to actually delete files.
Remove untracked files AND directories
git clean -fd
-dincludes untracked directories (e.g., leftovernode_modules/ordist/).
Remove ignored files too (e.g., .env, build output in .gitignore)
git clean -fx
-xalso removes files ignored by.gitignore.
Interactive mode — choose what to delete
git clean -iOptions Summary
| Flag | Meaning |
|---|---|
-n | Dry run — preview only |
-f | Force delete (required) |
-d | Include untracked directories |
-x | Include ignored files |
-i | Interactive selection |
Clean Flow
flowchart LR
A[Untracked Files] --> B{Preview first?}
B -->|Yes| C[git clean -n]
C --> D[git clean -f/-fd]
B -->|No, risky| D
D --> E[Files permanently deleted]
Important Warning
Warning
git clean permanently deletes files — there is no undo. Deleted files are not moved to trash and cannot be recovered via Git. Always run git clean -n first to review what will be removed before using -f.