Removing files from git that match the gitignore file

 

Recently a developer accidentally committed and pushed a metric-ton of binary files into our repo that were supposed to be ignored by the gitignore file.

After a bunch of work trying different options I came across a solution.  We used the Git Bash (on windows) and ran the following:

git ls-files -i -z –exclude-from=.gitignore | xargs -0 git rm –cache

This effectively told git to remove all files that matched the ignore list.  It’s not perfect, but it saved us a ton of time removing files from a hundred different folders.

Advertisements