Skip to main content
Stashing & Cleaning

git clean

Remove untracked files from the working directory

Practical Example

git clean -n                     # preview what will be removed
git clean -fd                    # remove untracked files and directories
git clean -fdx                   # remove everything not tracked

Command Overview & How It Works

Deletes files that are not tracked by Git — typically build artifacts, logs, or generated files that are not in your .gitignore. This is a destructive command, so always preview first with git clean -n (or --dry-run), which shows what would be deleted without actually removing anything. Use git clean -f to force removal of files, git clean -fd to also remove untracked directories, and git clean -fx to remove ignored files (those matching .gitignore) as well. For maximum cleaning, git clean -fdx removes untracked files, directories, and ignored files — useful for wiping a build directory before a fresh build. Combine with git reset --hard for a complete reset to the last committed state.

Practical Tips

  • Always preview with git clean -n before deleting anything — git clean cannot be undone.
  • Use git clean -fdx to wipe untracked files, directories, and ignored files before a fresh build.
  • Combine with git reset --hard for a complete return to the last committed state.

Related Commands in Stashing & Cleaning