I'm splitting off part of a git repo to create a new repo, and am trying to use git filter-branch
to maintain the history of the files that are being moved to the new project. I know about --subdirectory-filter
but this is not a good solution because the files I'm pulling out don't map cleanly to one subdirectory. The best option I've found so far is --index-filter
, used as follows:
git filter-branch -f --index-filter 'git read-tree --empty && git reset -q "${GIT_COMMIT}" -- <list of files>' --prune-empty -f
This seems to work, except I'd like to be able to programmatically generate the list of files to keep so I can iteratively refine this list. I'm currently trying to get a list of the files I want to keep in another file, and append this to the string representing the command to be executed for each commit as follows:
tmp=$(cat ~/to_keep.txt) && git filter-branch -f --index-filter 'git read-tree --empty && git reset -q "${GIT_COMMIT}" -- '$tmp --prune-empty -f
Unfortunately, this results in
fatal: bad flag '--prune-empty' used after filename
Even just echoing the files seems to cause trouble:
tmp=$(echo a.txt b.txt) && git filter-branch -f --index-filter 'git read-tree --empty && git reset -q "${GIT_COMMIT}" -- '$tmp --prune-empty -f
fatal: ambiguous argument 'b.txt': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
I've also tried concatenating the strings earlier:
tmp1=$(echo a.txt b.txt) && tmp2='git read-tree --empty && git reset -q "${GIT_COMMIT}" -- ' && tmp3=${tmp2}${tmp1} && git filter-branch -f --index-filter $tmp3 --prune-empty -f
fatal: ambiguous argument 'read-tree': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
I assume this is just concatenation not happening as I expect in the shell. Does anyone know how I can make this work? It would be great if you could explain what these errors mean, as well. Thanks.