I use Git Subtree like below:
git subtree add --prefix=directory_destination_path --squash git@bitbucket.org: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?
Suppose you wanted to add the
images/
prefix of the octocat git repo at the refmaster
.Suppose we want to use the remote hosted at https://github.com/octocat/octocat.github.io.git (Note: GitHub returns
error: Server does not allow request for unadvertised object
in the followingfetch
command is you specify a sha1 not associated with named ref)Starting on the branch you wish to add the subtree to, let's first fetch the desired commit history and checkout our new branch
Next, let's create a new history where only the files under our desired prefix (
images/
) exist.Finally, let's add this subtree to our desired branch (presumably the last branch you were on,
git checkout -
)Now you should have all the desired files on your current branch with a full git history.
Alt Squashed history
At time you want the commits in our subtree to be squashed into a single commit. This to a certain extent defeats the purposes of adding a subtree but it has it's place. The following is a variation of what is described above to limit the history pulled into your repo
Starting on the branch you wish to add the subtree to:
Note: we are specifying
--depth=1
in the above since we are using the--squash
is the followinggit subtree split
command.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 repodirectory_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.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.There should now be a branch called
temp_branch
containing just the directory you are interested in. Now you can perform agit-subtree-merge
to bring it all into your repo.This should merge in the temp_branch into your master branch.