Git: Delete all tracked files

githome-directory

I have a directory containing a git repo as well as other unrelated files which I do not want tracked (its my home directory, sorry if this is bad practice–alternative recommendations for versioning bash profile and vim files are welcome!)

I can easily remove the .git directory, but that leaves me with all the tracked files in my directory. How do I delete all tracked files so I can completely remove the git repo and its relevant files, effectively "uncloning" or "uninstalling"?

In addition, after removing these files, how do I remove any directories that used to contain tracked files but are now empty?

Related (opposite question): Git: How to delete all untracked files

Best Answer

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.

Related Question