3

I want to do the equivalent of git-subtree split only on a single file rather than a directory. That is, I want to make a new repository having only that one file and all its history.

Passing the filename to the --prefix argument does not work

git subtree split --prefix=file_name -b split

I tried a couple variations on git-filter-branch but did not find the correct incantation.

Vampire
  • 35,631
  • 4
  • 76
  • 102
vossad01
  • 11,552
  • 8
  • 56
  • 109
  • [See this related (but not quite the same) question](http://stackoverflow.com/q/42661978/1256452). To achieve what you want, you must copy the commits you wish to copy, and not any of the others. You *can* do this with `git filter-branch`, it's just slow and painful. You can do it other ways that will be faster; until you write a script like `git subtree`, it will still be painful, though. – torek Mar 24 '17 at 16:17
  • Here is a solution that works for one or more files: https://stackoverflow.com/a/6006679/1072626 – vossad01 Jun 08 '17 at 18:24

1 Answers1

3

You could use filter-branch with a tree-filter deleting everything except the file you want to keep like

git filter-branch --tree-filter 'find -type f -not -path ./foo/bar -delete' --prune-empty
Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Note that it is critical to include the `./` at the start of the file path. – vossad01 Mar 24 '17 at 18:13
  • I then used [this invocation of filter-branch](http://stackoverflow.com/a/6149972/1072626) to drop the root commit. In my case that file I wanted was not in the root commit but the commit was preserved as an empty commit. – vossad01 Mar 24 '17 at 18:15