Nested git repositories without remotes (a.k.a. git submodule without remotes)

前端 未结 3 1781
长发绾君心
长发绾君心 2020-12-01 01:27

I have a project of which I am interested in breaking out portions as open-source. I\'ve set up nested git repositories main, one, two and three:

main/
├── on         


        
相关标签:
3条回答
  • 2020-12-01 01:57

    I just came up with the idea to place the repository of the submodules exactly as a subdirectory of the superproject. That way, the project as a whole is only dependent on the one repository/ location that you set up (based on the answer from Chris).

    E.g. Submodule one would have as its main repository the location server:/path/to/your/main/one (via git remote -v in that dir).

    That way the submodule functionality (e.g. git submodule update --recursive) is pointed to the right location as well since the URL of that module/repository points to ./one (.gitmodules).

    0 讨论(0)
  • 2020-12-01 02:00

    Chris's answer don't assume for a "master" repo (master being the default name of the main branch in Git, not a repo).

    It declares submodules in a "parent" repo, which in your case would be "main".

    So you need three independent repo "one" "two" and "three" setup elsewhere, and then clone and add them to your "main" repo as submodules:

    # first remove the one directory previously added
    git rm --cached one
    # then declare the external one repo as a submodule in "main" repo)
    git submodule add /path/to/one.git one
    
    0 讨论(0)
  • 2020-12-01 02:10

    You can do what you want, but your one, two, and three would need to be accessible to whoever will need to clone them—this is not usually the case for “random” development repositories.

    If you do set this up, you will need to be very careful not to delete “your” repository (or make it otherwise inaccessible) since it is not just “yours”: it will be origin in your collaborator’s clones and it will serve as the “central”/“upstream” repository (as specified in .gitmodules).


    If all your collaborators are local (and can read from the repositories), you can add your existing sub-repositories as submodules by giving their local paths as URLs:

    git rm --cached one two three
    git submodule add `pwd`/one
    git submodule add `pwd`/two
    git submodule add `pwd`/three
    

    If not all your collaborators will be working on the same machine, then that will probably not work correctly (since it will store a local path in .gitmodules; non-local collaborators would have to adjust the URLs after git submodule init).

    If your one, two, and three are remotely Git-accessible, then you can specify their effecive URLs instead:

    git rm --cached one two three
    git submodule add server:/path/to/your/main/one
    git submodule add server:/path/to/your/main/two
    git submodule add server:/path/to/your/main/three
    

    In both cases, since you already have a sub-repository in place, git submodule add will use that instead of trying to clone from the specified path/URL.

    0 讨论(0)
提交回复
热议问题