Skip to main content

Git Clean Untracked Files Guide

When to Use

When many untracked files or directories appear in the repository -- build artifacts, temporary files, downloaded data, test outputs -- and you're sure you don't need them, use git clean.

This page handles:

  • Untracked files
  • Untracked directories
  • Optionally, ignored files

It does not modify files already tracked by Git.

Check Current State First

git status --short
git clean -nd
git clean -ndX
  • git clean -nd -- preview only, no deletion. Run this first.
  • git clean -ndX -- preview only what "ignored files" would be deleted.
ScenarioRecommended CommandResult
Preview untracked files to be deletedgit clean -ndSafe preview, no deletion
Delete untracked files and directoriesgit clean -fdClean up normal untracked content
Delete ignored files toogit clean -xfdMost thorough, also most dangerous
Interactive selection for deletiongit clean -idConfirm each item, more cautious

Common Scenarios

Preview First, Then Delete Untracked Content

git clean -nd
git clean -fd

Only Clean Ignored Build Artifacts

git clean -fdX

-X means "only delete ignored files" -- suitable for cleaning dist/, build/, .cache/, etc.

Restore Repository to a Very Clean State

git restore .
git clean -fd

If there are staged changes, run first:

git restore --staged .

Risks and Boundaries

  • git clean deletes real files on disk -- they don't go to the recycle bin.
  • git clean -xfd will also delete ignored files, commonly things like node_modules/, .env, and build cache directories.
  • By default, git clean requires -f to actually execute -- this is an intentional Git safeguard.
  • It does not handle modifications to tracked files; for that, see Discard Working Directory Changes.
git status --short --ignored
git clean -nd
git clean -fdX
git stash push -u -m "backup before clean"