Git clean: remove untracked files
Temporary files, logs, builds cluttering your project? git clean tidies up your working directory by removing everything that isn't tracked by Git.
What is git clean?
git clean removes untracked files from your working directory. These are files Git doesn't know about: they were never added with git add.
It's useful for starting fresh: removing temporary files, debug logs, build artifacts.
Warning: git clean is irreversible. Deleted files cannot be recovered. Always use -n (dry run) before -f (force).
Git clean options
git clean -nDry run: shows what would be removed
git clean -fForce: removes untracked files
git clean -fdRemoves untracked files AND directories
git clean -fxAlso removes ignored files (.gitignore)
git clean -iInteractive mode: choose file by file
Git clean in practice
Always check with -n before deleting with -f.
Dry run then clean
With directories and ignored files
When to use git clean?
Clean up after a build
Remove build artifacts (dist/, build/, .cache/) to start fresh before a new build.
Remove test files
Temporary files created during debugging (logs, exports, screenshots) that are not meant to be committed.
Start from scratch
Combined with git checkout ., you can return to a state identical to the last commit, with no extra files.
Safety rules with git clean
Always -n before -f
Run git clean -n to see what will be deleted. Then git clean -f only if the list is correct.
Be careful with -x
The -x flag also removes files ignored by .gitignore. This includes .env, node_modules, etc. Use it with caution.
Prefer interactive mode
git clean -i lets you choose file by file what to delete. Slower but safer.
Check your .gitignore
Make sure your .gitignore is up to date. Files that are neither ignored nor tracked will be removed by git clean.
Part of the Undo Changes in Git guide
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about git clean
Learn to clean your repo safely
GitQuest offers scenarios where you need to use git clean in a safe environment.
Start practicing now