multiple git repositories cloned into the same directory

12,171

Solution 1

Here's a suggested slight change to your desired deploy commands:

cd /var/www/  
git clone /home/repo/.git newsite
git remote add mod1 /home/modules/mod1/.git  
git remote add mod2 /home/moudles/mod2/.git  

cd newsite
git merge mod1/master
git merge mod2/master

Explanation:

As you said, your main module (/home/repo) and your mod1, mod2 etc. have the same folder structure. Submodules then aren't really appropriate since a submodule has to live in a subdirectory of your main repo. In this version, your local git repository (created by the clone) knows about three remote repositories. After the merge operations, its state won't be the same as any of them. The merge of mod1 will bring in any new files "owned" by mod1. You may get merge conflicts if any files in home/repo have the same name as those in mod1, etc. You could supply a flag to merge to select the version from mod1 always, which I believe you said is the behavior you want.

Solution 2

In short, you cannot have one git repo to be as sudirectory of another.

There is one exception to this rule: you can have one git repo as submodule of another, but this has a lot of limitations and inconveniences.

General approach to this is to have one master git repo which has no actual convent of its own, but only tracks few git repos as its submodules.

Alternatively, I would recommend using repo tool developed by Android project exactly for this purpose. It involves creating small git repository containing just one XML file (called manifest) which tracks where your sub-projects are checked out into and how they are glued together. This works really well on Linux and Mac, but unfortunately does not support Windows (repo requires symbolic link support by OS).

In certain sense, git submodules and android repo solve essentially same task, but using slightly different means.

Solution 3

The vcsh tool allows maintaining multiple repos in the same directory.

Share:
12,171

Related videos on Youtube

Jamie
Author by

Jamie

Updated on June 14, 2022

Comments

  • Jamie
    Jamie over 1 year

    I've got a standard repository for my project

    /home/repo/.git  
    

    this is the repository i clone to get the base code for new websites i.e. i cloned this to

    /var/www/site1
    

    i also have several modules that i've created as repositories, some websites will be using these modules and some will not.

    /home/modules/mod1/.git  
    /home/modules/mod2/.git
    

    is there a way that i can clone those modules into the same site folder?

    /var/www/site1  
    

    the module directories are set up with the same folder structure as the master repo, when i clone them on top of the master repo clone they should merge/replace existing files. (rarely any file overlap)

    my optimal solution would be naming the repo's in some way so that when i deploy a new site I do something like:

    cd /var/www/newsite  
    git clone /home/repo/.git  
    git clone /home/modules/mod1/.git  
    git clone /home/moudles/mod2/.git  
    

    and when I have updates to make to the site I could do a pull like:

    git pull origin master  
    git pull mod1  
    git pull mod2  
    

    or preferably:

    git pull origin master 
    

    would also call the pulls on mod1 and mod2.

    I've been looking at git submodules and branches but can't figure out if they are what I need.

    • tiago
      tiago about 11 years
      Please ident code/commands with 4 spaces and do some spell checking. And by the way, in English, 'I' is always capitalised.
  • Lazy Badger
    Lazy Badger about 11 years
    "There is one exception to this rule" - two. And git-tree is a lot better alternative to submodule
  • Lazy Badger
    Lazy Badger about 11 years
    git-subtree, sorry - github.com/apenwarr/git-subtree
  • knittl
    knittl about 11 years
    You must cd into the repository before you can add remotes. Also, you must fetch first, before you can merge ;) – but I don't think this will work well for the OP, because the question mentions »rarely any file overlap« (which tells me, that there are overlaps).
  • mvp
    mvp about 11 years
    As good as git-subtree is, it still cannot replace external git repos that you need and which are totally outside of your control (without changing commit ids and necessity to merge back and forth). Also, even in latest git, it is not enabled by default
  • Jamie
    Jamie about 11 years
    I think this will work out very well for the project. When you say I can "supply a flag to merge to select the version from mod1 always", would that be including all files from mod2 except for the overlap file? or would that eliminate mod2 from the merge completely?