git: Delete all tracked files?

Solution 1:

For files you might want to change your command line slightly to the suggested command-line on the git-rm page

git ls-files -z | xargs -0 rm -f

This is much safer with more complex file paths. For directories you could try a similar strategy:

git ls-tree --name-only -d -r -z HEAD | sort -rz | xargs -0 rmdir 

It does however depend on how you would like to treat directories that contain files (often gitignored) that are untracked. The command line above should leave these files and their directories.

But it would be easy to change this to delete the directory, whatever the contents.

Solution 2:

You can ask git for a list of all tracked files with git ls-files. So, the following command should work just fine

git ls-files | xargs rm