Git & GitHub Interview Question #124

What is git clean and why should it be used carefully?

Advanced GitHub Actions, Repository Internals, Submodules, LFS & Diagnostic Tools Senior Advanced

Quick Interview Answer

git clean removes untracked files from the working tree. Because those files are not tracked by Git, Git may not be able to restore them after deletion.

Detailed Explanation

git clean removes untracked files from the working tree. Because those files are not tracked by Git, Git may not be able to restore them after deletion.

Use the dry-run option first to see what would be removed. Additional flags are required for directories or ignored files, and destructive cleanup should never be run blindly in a workspace containing important untracked data.

Code Example

# Preview only
git clean -n

# Remove untracked files
git clean -f

# Preview untracked directories too
git clean -nd