Search code examples
gitgit-clone

Git - clone only part of the repository and get pulls only for that part?


So let say I have two repositories with specific modules (or subdirectories) like this:

repo1/
  /module1
  /module2
  /module3


repo2/
  /module4
  /module5

So on my test server I load these repositories like it is and it is OK, because I get all the code. But on client server, let say I only need his project modules which let say is repo2, but also one module from previous project which is repo1.

Is there a way I could clone from repo1 only module1 and then if I would update anything in repo1 that is changed in module1, when I would do pull for repo1 (which would have only part of repo cloned), it would update it?

Update So on client server there would be two repos like this:

repo1/
  /module1

repo2/
  /module4
  /module5

Solution

  • You can make a sparse checkout for each repos:

    mkdir repo1
    cd myrepo1
    git init
    git config core.sparseCheckout true
    git remote add -f origin /url/of/repo1
    echo module1/*> .git/info/sparse-checkout
    git fetch
    git checkout master
    

    (same for repo2)