As stated in the document here :
I believe that the problem is that the merge commands do not actually touch this subdirectory, which means that the filter for subdirectories does not even look at them and therefore cannot save them. Therefore, we must use a different filter that analyzes each commit. tree-filter is perfect for this, but we need to write a script to do what we want to do with each commit.
Also, my problem was actually a little wider than in the example, since there were siblings in my project-root
folder that I wanted to delete. The subdirectory filter will delete them, but for this, using the tree filter, it was useful for find
come to the rescue.
It was convenient to run commands every time in a separate file.
for a repo structured as follows:
.git/ someDirectory/ someFile.txt otherDirectory/ dontDeleteThisOne/ project-root/ xyz/ abc/
This is what worked for me:
git filter-branch --tree-filter /absolute/path/to/filterScript.sh --tag-name-filter cat -- --all
where / absolute / path / to / filterScript.sh is the executable script file containing this:
#!/bin/bash
The resulting repo structure is as follows:
.git/ dontDeleteThisOne/ xyz/ abc/
This result is equivalent to the result of git filter-branch --subdirectory-filter project-root
, EXCLUSIVELY that the merge of commits is saved in the history as desired.
Of course, this takes MUCH more than using a subdirectory filter ...