It sounds like you want a single history containing all changes to files in the two folders. You can't do that with --directory-filter
, but you can still use filter-branch
with the --index-filter
.
git filter-branch --index-filter 'git rm --cached -r --ignore-unmatch -- :/:* :/!:Folder-1 :/!:Folder-2` --prune-empty -- --all
Using git rm
as an index-filter
creates a new commit from each original commit by loading the original commit to the index, removing some files, and committing the new index.
Each of the :/!:...
options tells git rm
not to delete the named folder and its contents, and :/:*
tells it to delete everything else.
The --prune-empty
option eliminates from the new history any commit that only changed files outside the folders you keep - because those would look like they didn't do anything in the new history. You can omit this option if you want to keep those commits anyway.
The above keeps the files in their original directories. If you want something different, then you may need to use a --tree-filter
instead of --index-filter
. That option is considerably slower (especially if the repo is large), but it's easier to make more complex changes (because the filter script just has to operate on files on disk, rather than directly transform the index).
That said, as of sometime recently the documentation recommends against using filter-branch
. I'm not yet familiar with the tool they suggest using instead.
So, here's the filter-branch
docs, from which you can either follow the warning and look up a different tool, or learn more about the above procedure: https://git-scm.com/docs/git-filter-branch