Git: How to delete all untracked files?

I'm using Git for version control. Now I want to delete on my local machine all files in the repository that are not tracked.

I can use

git status

to list these files, but how can I delete all of them?


If you have it in ignore, use git clean -xf. You can do git clean -df but that will also remove un-tracked directories. Use -n for a dry-run.

See cleaning up un-tracked


git clean -f

User interactive approach:

git clean -i -fd

Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/amazon/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/s3/ [y/N]? y

-i for interactive
-f for file
-d for directory

Note: Add -n or --dry-run to just check what it will do.


There is a subtlety worth mentioning about git clean -f vis-a-vis untracked files and directories. If you have an untracked directory that contains files which are, a fortiori, untracked, then git clean -f will NOT delete those untracked files.

In other words, it is NOT always the case that git clean -f will delete all untracked files. A better explanation of git clean -f is that it deletes all untracked files that are not in untracked directories.

git clean -f -d must be used to delete untracked files that are in untracked directories and there appears to be no way to delete all untracked files without also deleting untracked directories that only contain untracked files.

Use git clean -f -d -n to REALLY see what you want to do to restore your working directory to what it would be without any untracked files. Then use git clean -f -d to do that.