Git Subtree only one file or directory

kicaj picture kicaj · Mar 11, 2014 · Viewed 11.6k times · Source

I use Git Subtree like below:

git subtree add --prefix=directory_destination_path --squash [email protected]:kicaj/projectname.git master

But in path: directory_destination_path copy all repo from projectname.git

How to copy to directory_destination_path only subdirectory or only some file from projectname.git ?

EDIT:
One more question:
How to update (automatic) files changes in both repositories were still the same? It is possible?

Answer

eddiemoya picture eddiemoya · Mar 11, 2014

If I understand, you seem to want to only merge in a certain directory of a different repository, and you want it to be a subtree in your repository. I am going to call the directory of interest in the project.git path_of_interest_in_project and call the destination in your repo directory_desination_path.

Try adding the remote project.git as a remote, then checking out one of its branches locally. Then use git-subtree split to split out just the directory of project.git you are interested in. After that merge it into your repo using subtree merge.

git remote add project [email protected]:kicaj/projectname.git
git branch project_master project/master

The branch project_master should now store the entire history of your project.git repo.

Then you'll need to use the git-subtrees-split process.

git checkout -f project_master
git subtree split --squash --prefix=path_of_interest_in_project -b temp_branch

There should now be a branch called temp_branch containing just the directory you are interested in. Now you can perform a git-subtree-merge to bring it all into your repo.

git checkout -f master
git subtree merge --allow-unrelated-histories --prefix=directory_destination_path temp_branch

This should merge in the temp_branch into your master branch.