Skip to main content
Git Clean: Remove Untracked Files and Directories
Tutorial4 min read

Git Clean: Remove Untracked Files and Directories

When to Use Git Clean

Your working directory accumulates untracked files — build output, generated configs, downloaded assets, temp files. `git clean` removes them all at once.

Safety First: Dry Run

Always preview what will be deleted:

git clean -n

# Would remove: temp.log, dist/bundle.js

Removing Files

# Remove untracked files

git clean -f

# Remove untracked files and directories

git clean -fd

# Remove ignored files (those in .gitignore)

git clean -fX

# Remove everything (untracked + ignored)

git clean -fxd

Interactive Mode

git clean -i

# Interactive prompt for each file

# Choose: clean, skip, or quit

Git Clean vs Git Reset

`git reset --hard` removes changes to tracked files but leaves untracked files alone. `git clean` removes untracked files. Together they create a completely clean working directory:

git reset --hard

git clean -fd

Use Cases

**Before a clean build:** Remove `dist/`, `node_modules/`, and `*.cache` directories.

**After merging:** Clean up generated files from the merge process.

**Before switching contexts:** Ensure no leftover files from the previous task.

**After running generators:** Remove scaffolding files that were only needed for setup.

Warning

`git clean` permanently deletes files. Unlike `git reset`, there is no way to recover cleaned files — they were never tracked by Git. Always use `-n` (dry run) first.

A clean working directory reduces confusion and ensures reproducible builds.