How to “git subtree” only one file or directory?

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.

Leave a Comment