What is git clean?
Why Interviewers Ask This
This tests whether you can apply Git & GitHub knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
git clean removes untracked files and directories from the working tree. It is the complement to git restore (which handles tracked files). Since it permanently deletes files, always use -n first. Commands: git clean -n / --dry-run — preview what would be removed (safe — doesn't delete); git clean -f — delete untracked files (required flag — no accidental cleaning); git clean -fd — also delete untracked directories; git clean -fx — also delete ignored files (e.g., node_modules, build output); git clean -fdx — delete untracked files, directories, AND ignored files. Interactive: git clean -i — interactively choose what to remove. -e pattern — exclude pattern from cleaning. Use cases: (1) Clean build artifacts before a fresh build; (2) Remove generated files; (3) Reset to a pristine working state (combine with git reset --hard HEAD). WARNING: deleted files are NOT recoverable through Git — they aren't committed, so no history. Double-check with -n before -f. Comparison: git restore . reverts modified tracked files; git clean -fd removes untracked files; together they fully reset the working directory.
Pro Tip
This topic has Git & GitHub-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.