Git: Create repo as submodule
I’d like to create a new repository as a submodule of my project.
Usually, I create a Github repo and then add it as a submodule using the command
git submodule add url_to_repo.git
Is there a way to create a new repo directly as a submodule without creating the repo somewhere else first (neither locally nor remote e.g. on Github)?
3 Solutions collect form web for “Git: Create repo as submodule”
I don’t see how you could: a submodule is by definition a SHA1 from another repo (ie the other repo must exist for the parent repo to extract said SHA1): you must have its address referenced in the
.gitmodules file that you keep in the parent repo.
Submodules are composed from a so-called gitlink tree entry in the main repository that refers to a particular commit object within the inner repository that is completely separate.
Defines a URL from which the submodule repository can be cloned. This may be either an absolute URL ready to be passed to git-clone(1) or (if it begins with
../) a location relative to the superproject’s origin repository.
So you could create the submodule repo locally, but you must create it anyway.
If I understand you right this is what I’m frequently doing for eclipse projects and workspaces. Let’s start with this structure:
$ find . . ./projekt.txt ./sub1 ./sub1/sub1.txt ./sub2 ./sub2/sub2.txt
First initialize the submodules and master:
$ cd sub1 $ git init $ git add * $ git commit -m "init sub1" $ cd ../sub2 $ git init $ git add * $ git commit -m "init sub2" $ cd .. $ git init $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # projekt.txt # sub1/ # sub2/
To add those folders as submodules instead of regular folders, simply do the following command, and use relative paths like
./sub instead of just
$ git submodule add ./sub1 $ git submodule add ./sub2
Now it should look like
$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: .gitmodules # new file: sub1 # new file: sub2 # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # projekt.txt
Finally do a
git add * and
git commit -m "init parent" on the parent folder and there you are!
If you now change a file in one of your submodules, you have to commit the submodules first and then the parent repository as well in order to get the latest versions of your submodules when someone clones your parent repo.
submodule_dir is the name of the directory you wish to submodule-ize (assuming it’s not already under git control).
cd submodule_dir git init git add . git commit # on github, create the new repo, then: git remote add origin firstname.lastname@example.org:your_username/your_repo_name.git git push -u origin master cd .. mv submodule_dir submodule_dir_delete_me git submodule add email@example.com:your_username/your_repo_name.git submodule_dir
Later (once you’re happy)
rm -rf submodule_dir_delete_me