1

I know there is a big thread Detach subdirectory into separate Git repository but there are always some variations. My task differentiates from the above question is that there are some files instead of a subdirectory.

Ex.

XYZ/
    .git/
    XY1/
    ABC/foo.py
    ABC/bar.py
    ABC/asdf.py
    ABC/...

And I would like to extract foo.py and bar.py into a separate Git repository.

My first trial is to make a subdirectory for these 2 files and make use of method mentioned in 1:

$ mkdir foobar
$ git mv ABC/foo.py ABC/bar.py foobar
$ git commit -m '...'
$ git filter-branch --subdirectory-filter foobar HEAD

So far so good except the commit history is lost. Any idea for that?

Community
  • 1
  • 1
Drake Guan
  • 14,514
  • 15
  • 67
  • 94

2 Answers2

0

You have to use the subtree merge strategy to merge the "distant" folder "ABC" into your local folder "ABC" and maintain history. Then you will use filter-branch to remove unwanted files.

Ghislain Leveque
  • 958
  • 1
  • 8
  • 21
0

What I did to accomplish this was:

  1. git clone ABC/ DEF/; cd DEF
  2. git remote rm origin
  3. git filter-branch --prune-empty --tree-filter 'rm -fr <list of files/folders to remove>' -- --all
  4. git filter-branch -f --prune-empty --subdirectory-filter DEF -- --all

And that was it. Added new project remote, and continued working in separate repository.

Srecko
  • 1