Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/ ├── one ├── three └── two 

I thought that by going into "main" and doing

git add one git add two git add three 

(note the lack of trailing slashes), I'd set up submodules with the sub-repositories and be good to go.

However, as noted in How to track untracked content?, this only creates gitlinks and not real submodules.

Unfortunately, that answer doesn't help, as it assumes that there is a "master" repository somewhere else for "main/one", "main/two", and "main/three". I'd like these sub-repo's to be the master repositories. I'm considering fake submodules (as per Git fake submodules), but that's not a particularly ideal situation for cloning.

Any other suggestions out there?

like image 480
David Alan Hjelle Avatar asked May 23 '11 17:05

David Alan Hjelle


People also ask

Can git repositories be nested?

Git allows you to include other Git repositories called submodules into a repository. This allows you to track changes in several repositories via a central one. Submodules are Git repositories nested inside a parent Git repository at a specific path in the parent repository's working directory.

What are git submodules used for?

Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.

Is using git submodules a good idea?

Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.

Does git clone include submodules?

Cloning a Project with Submodules If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.


2 Answers

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.

like image 187
Chris Johnsen Avatar answered Sep 28 '22 08:09

Chris Johnsen


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 
like image 25
VonC Avatar answered Sep 28 '22 10:09

VonC