I have a complex git repo from which I would like to delete ALL files and history except for two folders, let's say:
foo/a
bar/x/y
While git filter-branch --subdirectory-filter
would let me select one folder, and make that the new root, it doesn't seem to give me any option for selecting two directories, and preserving their placement.
git filter-branch --tree-filter
or --index-filter
seem like it will let me iterate through every commit in history, where I can use git rm
on an unwanted folder.
I cannot seem to find any working way to get these commands to just preserve the two folders I desire while clearing everything else.
Thanks!
For files, I've done this with
git fast-export
. But I'm not sure that would work recurseively on directories. So I'd suggest using a combination ofgit fast-export
andfind
.Then create a new repo, and import the streams.
You are correct: a tree filter or an index filter would be the way to do this with
git filter-branch
.The tree filter is easier, but much slower (easily 10 to 100 times slower). The way a tree filter works is that your supplied command is run in a temporary directory that contains all, and only, the files that were present in the original (now being copied) commit. Any files your command leaves behind, remain in the copied commit. Any files your command creates in the temporary directory, are also in the copied commit. (You may create or remove directories within the temporary directory with no effect either way, since Git stores only the files.) Hence, to remove everything except A and B, write a command that removes every file that is in something other than either A or B:
for instance.
The index filter is harder, but faster because Git does not have to copy all the files out to a file tree and then re-scan the file tree to build a new index, in order to copy the original commit. Instead, it provides only an index, which you can then manipulate with commands like
git rm -rf --cached --ignore-unmatch
for instance, orgit update-index
for the most general case. But, now the only tools you have are those in Git that manipulate the index. There is no fancy Unixfind
command.You do, of course, have
git ls-files
, which reads out the current contents of the index. Hence you can write a program in whatever language you like (I would use Python first here, probably, others might start with Perl) that in essence does:If you are willing to trust that no file name has an embedded newline, the above can be done in regular shell as:
which is not terribly efficient (one
git rm --cached
per path!) but should work "out of the box" as an--index-filter
.(Untested, but probably works and should be significantly more efficient: pipe
git ls-files
output throughgrep -v
to remove desired files, and pipegrep
output intogit update-index --force-remove --stdin
. This still assumes no newlines in path names.)