git clean

Remove untracked files and directories from your working directory, cleaning up build artifacts and temporary files.

Your working directory is cluttered with build artifacts, temporary files, and other untracked junk that you want to delete. git clean removes untracked files (files Git doesn't know about), giving you a pristine working directory that matches what's in version control.

Because clean permanently deletes files, Git requires the -f (force) flag to actually remove anything. Running git clean -f deletes untracked files in the current directory. To also remove untracked directories, add -d: git clean -fd. This is common after builds or tests that create temporary directories.

Before deleting anything, run git clean -n (dry run) to see what would be removed. This safety check prevents accidentally deleting files you meant to keep. If the list looks right, run the same command with -f instead of -n to actually delete them.

By default, clean respects .gitignore and won't remove ignored files. To also remove ignored files (like build artifacts explicitly ignored), use -x: git clean -fdx. This is useful when you want to completely reset your working directory to a clean state, removing everything that isn't tracked.

Interactive mode with -i prompts you for each file or directory, letting you select what to delete. This is safer than force-deleting everything at once, giving you fine-grained control over what stays and what goes.

In pull request workflows, clean helps when switching between branches with very different build outputs or generated files. After checking out a different branch, clean removes files that don't exist in that branch's version, preventing old build artifacts from interfering with tests or causing confusion.

Understanding clean means knowing how to remove untracked files safely. It's a powerful cleanup tool, but its destructive nature requires caution. Always dry-run first, and make sure you're not about to delete work in progress that just isn't tracked yet.