Is it possible to merge a subtree from a subdirectory?

Suppose there are two git repositories with some files in them:

ProjectA + a + b ProjectB + foo/x + foo/y + bar/z 

Now I want to include the foo directory from ProjectB into ProjectA.

As far as I understand, when I do git subtree add , prefix is the path it should have in the receiving repository, so when I do

 git subtree add --prefix=project_b --squash URL_of_ProjectB 

I would end with

 ProjectA + a + b + project_b + foo/x + foo/y + bar/z 

Is there any way to indicate that I only need foo to get this layout?

 ProjectA + a + b + project_b + x + y 
+6
source share
1 answer

I think this is not possible with git subtree . Maybe git filter-branch is what you need:

 git remote add b <URL_TO_B> git fetch b git checkout -b b_master b/master 

Rewrite history b containing only foo :

 git filter-branch --prune-empty --subdirectory-filter foo b_master 

or use git subtree split

 git subtree split -P foo --branch foo 

and add it as a subtree

 git subtree add --prefix=project_b --squash b_master or foo 
+5
source

Source: https://habr.com/ru/post/945227/


All Articles