git clean
git clean: Clean the working directory by removing untracked files.
In Git, the git clean command is used to clean up your working directory by removing files that are not tracked by Git. Untracked files are those that haven't been added to the version control's tracking system, typically because they are temporary or generated files that don't need to be included in the project history. Think of files like compiled binaries, temporary logs, or project files that clutter your workspace but aren't necessary for version control.
Using git clean rather than manually deleting files helps ensure you only remove untracked files, so you don't accidentally delete tracked files that are important to your project. This command can be useful when you want to reset your directory to a clean state before starting fresh with new changes.
If you're unsure what will disappear, run git status or the dry-run examples below to review untracked files first.
However, be cautious with git clean because it permanently deletes files. Once removed, they cannot be recovered from Git or your system's trash or recycle bin.
Examples
Run a clean up in interactive mode, which allows you to review each file before removing it:
git clean -iThe above command will ignore directories. To include directories, use the -d flag:
git clean -idIf you are sure of what you're doing, forcefully delete all untracked files:
git clean -fTo include directories as well:
git clean -fdSimulate a clean up, showing what files would be removed without actually deleting them:
git clean -nTo include directories in the simulation:
git clean -nd