Cleaning Untracked Files

git clean removes untracked files from the working tree. It can be useful for returning a build directory to a clean state, but it is destructive because untracked files are not recoverable through Git history.

Always use a dry run first with git clean -n.

Untracked Files Are Not Versioned

Git cannot recover an untracked file after git clean removes it. Always preview the operation first.

Preview Before Cleaning
git clean -n
git clean -nd
git clean -fd
💡 -n previews files, -d includes untracked directories, and -f performs the deletion. Never use destructive cleanup blindly.
Key Takeaways
  • git clean -n is the safest first step.
  • Untracked files are outside normal commit history.
  • Use .gitignore to prevent generated files from repeatedly appearing as untracked.
Knowledge Check

What does `git clean -n` do?