A simple way to detect unused files in a project using git
After finding that we had a few images checked into our project’s repository but that were not referenced in the project, I wanted to write a script to quickly see if there were any other unused assets.
This was a one-off script, so it probably won’t suit everyone’s needs, but here’s how we approached the problem:
First, we needed to get a list of the files that git was tracking in our image directory. While you could use ls for that, I wanted to be sure that we weren’t going to list any files that git was ignoring, so we started with git ls-files, whose output will look something like this if called as git ls-files ./img:
img/foo.png img/bar.png