Untracked files are files sitting in your working directory that Git hasn’t added to version control yet — think build artifacts, temp files, or new files you haven’t staged. The git clean command is the standard way to clear them out, but since deletions are permanent, it’s worth knowing exactly how it works before running it.
Check What’s Untracked First
Before removing anything, see what Git considers untracked. Running git status shows untracked files listed under the “Untracked files:” section. This gives you a quick sense of what would be affected before you touch the clean command at all.
Always Run a Dry Run First
git clean deletes permanently, bypassing anything like a recycle bin, so a preview step matters. The -n flag performs a dry run, showing which files would be removed without actually deleting them. By default, git clean won’t operate recursively on directories, which is another built-in safety mechanism against accidental permanent deletion.
Remove Untracked Files
Once you’re satisfied with the dry run output, you can proceed:
- Basic removal — the
-f flag forces the removal of untracked files, while leaving directories and ignored files alone.
- Include directories — add the
-d flag to also force removal of untracked directories.
- Include ignored files — add the
-x flag to remove untracked files that match entries in .gitignore.
- Target a specific path — pass a path along with
-f to remove untracked files from just that location rather than the whole directory.
Why Force Is Required
You might hit an error the first time you try this. Git is globally configured to require a “force” option before git clean will run, which acts as an important safety mechanism since the command isn’t undoable once executed.
Interactive Mode for More Control
If you don’t want an all-or-nothing deletion, git clean has an interactive option. It lists all files and directories set for removal and lets you filter by pattern, select specific files by number, confirm each file individually, or quit without cleaning anything. This is useful when a dry run shows a mix of files you want to keep and remove.
What Git Clean Won’t Do
It’s worth knowing where git clean's job ends. Running git reset --hard removes changes in files already under version control, but it won’t touch untracked files — those need git clean separately. If you’d rather set files aside temporarily instead of deleting them outright, git stash is the safer alternative to reach for.
Join The Discussion
Whether you’re cleaning up a messy branch before a merge or just tidying a local repo full of build artifacts, git clean tends to become a regular habit once you trust it. Have you run into a situation where a dry run saved you from deleting something important, or do you have your own workflow — aliases, git hooks, .gitignore patterns — for keeping untracked files from piling up in the first place?