How it works...

The git filter-branch command has different filter options depending on what needs to be done when rewriting the repository. In this example, where we are only removing files and folders from the repository; the index-filter is highly usable, as it allows us to rewrite the index just before recording a commit in the database without actually checking out the tree on disk, thereby saving a lot of disk I/O. The clean-tree script we prepared previously is then used to remove the unwanted files and folders from the index. First, we list the content of the index and filter the files and folders we want to keep. Then, we remove the remaining files and folders ($f) from the index with the following command:

git rm -rq --cached --ignore-unmatch ...

Get Git Version Control Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.