Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
thanks for the very helpful response. I suppose if I wanted to delete gitignore'd files, I could use git-clean – ILI4S K4RIM Jul 26 '12 at 15:32
you could. But what I was thinking is you might want to preserve directories that had non-tracked files in them. If you didn't you could be more aggressive about deleting the directories (rm -rf). The reason why I thought you wouldn't want to do this is that you could just remove all files not in the .git directory. – andygavin Jul 27 '12 at 11:38

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
share|improve this answer
Thanks! Not bad, modified to git ls-files | xargs -r rm it also deletes submodules. But how do I delete the empty directories which used to contain tracked files but are now empty? without simply deleting all empty directories within the working directory? (I have appended this to the original question) – ILI4S K4RIM Jun 29 '12 at 22:06
for example, after running this on my home directory, I am left with empty vim config directories: .vim/bundle, .vim/colors, and .vim/syntax – ILI4S K4RIM Jun 29 '12 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .