I'm looking for a way to rewrite the commits of my git repo so that only the commits related to certain files get preserved. The commits (and in fact the files) that do not belong to my criteria, I don't care if I lose them.
The files I want to preserve are now living on certain subdirectory of my repo, but at the beginning they were living at the root directory of it, then I moved them. I wish to preserve all the commit history related to this files, when they were living at the root, and when they got moved to their own subdirectory, and all their changes.
Suppose the commits related to this files do not touch anything but these files (if I find any commit not covering this requirement, I may use git rebase and split to achieve it).
Is there a way to achieve this, perhaps using filter-branch or something like that?
EDIT: all files I wish to preserve have some prefix in their names: 'foobar' for example. Every other file/subdirectory I wish to remove does not have this prefix.