Apply gitignore retrospectively

It often happens to me, that I added some files to a git-repository, which don't belong there. Normally you would ignore them using a .gitignore-file (By the way, is a great service for generating these files). But what If you already added them? You could manually delete each file using git rm --cached, but this could be quite annoying, for a large number of files. But, here's to the rescue:

git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached

This line applies your .gitignore-file retrospectively and removes all files affected by the current .gitignore. Awesome!