Skip to main content
Back to command reference
Stashing & Cleaning

git clean

Remove untracked files from the working directory

Practical example

bash
git clean -n                     # preview what will be removed
git clean -fd                    # remove untracked files and directories
git clean -fdx                   # remove everything not tracked

Command overview & how it works

Deletes files that are not tracked by Git — typically build artifacts, logs, or generated files that are not in your .gitignore. This is a destructive command, so always preview first with git clean -n (or --dry-run), which shows what would be deleted without actually removing anything. Use git clean -f to force removal of files, git clean -fd to also remove untracked directories, and git clean -fx to remove ignored files (those matching .gitignore) as well. For maximum cleaning, git clean -fdx removes untracked files, directories, and ignored files — useful for wiping a build directory before a fresh build. Combine with git reset --hard for a complete reset to the last committed state.

Chapter 07 · Disaster Recovery Drills

Disaster-proof your Git workflow with the Field Guide

Learn how to rescue deleted branches, revive detached HEADs, and undo bad rebases without panic. 97-page PDF + ePub — ₹499.

Learn more

Practical tips

  • Always preview with git clean -n before deleting anything — git clean cannot be undone.
  • Use git clean -fdx to wipe untracked files, directories, and ignored files before a fresh build.
  • Combine with git reset --hard for a complete return to the last committed state.

Deep-dive guides & tutorials

In-depth walkthroughs and practical scenarios related to git clean

All guides →

Related commands

More from Stashing & Cleaning