git: Delete all tracked files?

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

3 Answers

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.

3

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
2

To do this more efficiently,

git read-tree -u --reset $(:|git mktree)

or for purity points,

git read-tree -u --reset $(git hash-object -t tree /dev/null)

or even

git read-tree -u --reset 4b825dc642cb6eb9a060e54bf8d69288fbee4904

any of which will clean out all tracked files and all directories emptied along the way.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like