Git Clean Command: How to Remove Untracked Files Safely
The git clean command removes untracked files from your working tree. Because that can permanently delete local work, always preview what Git plans to remove before running a command that actually cleans files. This guide explains the common options and shows how to use them carefully.
What does git clean do?
git clean deletes untracked files—files in your repository that Git is not tracking. It does not remove tracked changes to files that are already in Git. For a first look at what Git considers untracked, run git status --short.
Unlike moving files to a system trash folder, a clean operation generally cannot be undone with Git. Uncommitted files that are not tracked or backed up may be lost, so inspect the preview before confirming deletion.
Preview untracked files before deleting them
Use the dry-run option -n (or --dry-run) to see which untracked files Git would remove without deleting them:
git clean -nCheck every listed path. If the preview includes anything you want to keep, stop and move or back up that work, or adjust the command to target only the files you intend to remove.
Remove untracked files and directories
Once the preview contains only files you are willing to delete, add -f to confirm the operation. Git requires this force option by default to prevent accidental cleanup:
git clean -f
git clean -nd
git clean -fdThe first command removes untracked files. Use -d to include untracked directories; the second command previews that broader cleanup, and the third performs it. Run the preview and deletion from the repository and path scope you actually intend to clean.
Git clean options: -n, -f, -d, -x, and -X
-nor--dry-run: show what would be removed without deleting it.-for--force: allow deletion. Git normally requires this option unless repository configuration changes that protection.-d: include untracked directories. Without it, Git generally leaves untracked directories alone.-x: also include files ignored by Git, such as build output or local environment files. This may delete useful local data; preview withgit clean -ndxfirst.-X: remove only ignored files, leaving other untracked files alone. Preview withgit clean -ndX.
Be especially cautious with -x and -X: ignored files are often intentionally excluded from version control, but they can still contain configuration or work you need.
Clean a specific path or use interactive mode
To limit cleanup to a path, add a pathspec after --. Preview it first, then repeat the same pathspec with -f only if the result is correct:
git clean -n -- path/to/generated/
git clean -fd -- path/to/generated/For a guided prompt that lets you select what to clean, use git clean -i. Interactive mode can be helpful when you want to inspect a group of candidates rather than remove all untracked files at once.
Git protects nested repositories by requiring additional force for their removal. Do not add repeated force flags unless you have specifically inspected and intend to remove a nested repository.
Common questions about git clean
Does git clean delete modified or staged files?
It targets untracked files, not modifications to tracked files or staged changes. Check git status to review those separately before cleaning.
How do I remove ignored files with git clean?
Use git clean -x to include ignored and untracked files, or git clean -X to include only ignored files. In both cases, add -n first to preview, and -d if you also want to include directories.
Can I undo git clean?
Git has no undo command for files deleted by git clean. Recover them from a backup or other recovery tool if available. Prevention is safer: preview with -n and keep important local files backed up or tracked appropriately.