Add subdirectory of remote repo with git-subtree

16,225

I was able to do something like this by adding :dirname to the read-tree command. (note that I'm actually just trying to learn git and git-subtrees myself this week, and trying to setup an environment similar to how I had my projects in subversion using svn:externals -- my point being that there might be a better or easier way than the commands I'm showing here...)

So for example, using your example structure above:

git remote add library_remote _URL_TO_LIBRARY_REPO_
git fetch library_remote
git checkout -b library_branch library_remote/master
git checkout master
git read-tree --prefix=dir1 -u library_branch:libdir
Share:
16,225
Yogu
Author by

Yogu

Hi! Occasionally, I start little projects such as curvy or Yogularm Infinte.

Updated on June 25, 2022

Comments

  • Yogu
    Yogu over 1 year

    Is there a way to add a subdirectory of a remote repository into a subdirectory of my repository with git-subtree?

    Suppose I have this main repository:

    /
        dir1
        dir2
    

    And this library repository:

    /
        libdir
            some-file
        some-file-to-be-ignored
    

    I want to import library/libdir into main/dir1 so that it looks like this:

    /
        dir1
            some-file
        dir2
    

    Using git-subtree, I can specify to import into dir1 with the --prefix argument, but can I also specify to only take the contents of a specific directory in the subtree?

    The reason for using git-subtree is that I can later synchronize the two repositories.