Reputation: 1995
I have a super repo and a few submodules in this repo:
MY_SUPER_REPO
Now I am creating a new directory that will later become a submodule:
When I am ready for it to become a sub repo I do:
git init
git remote add origin URL
I then go to the super repo and
git submodule add URL
Now .gitmodules
are updated accordingly BUT the .git
directory stays in the my_dir_3
and the hooks seems to be taken from here when I work in that sub module, not from the super repo .git/modules/xyz
directory. I run a script to copy hooks to .git/modules/xyz
in my super repo but since the local .git
folder exists in the added sub module the hooks in the super repo aren't used.
Is there any "magic" command to fix this or do I need to copy .git
manually?
Upvotes: 5
Views: 489
Reputation: 160
I have answered based on what I understood from your question, comment if there is anything you need.
Let's say you are creating a new directory which will later become a submodule. If you don't want to track the changes in this directory add this to .gitignore of you super_repo. Now, let's say you decide to make it a sub module, do this from you super_repo.
1) Remove the directory from .gitignore
2) Initialise your my_dir_3
git init
3)Add the submodule to your super repo
git submodule add module_name
Your submodule is added to the super repo. Things you should know to work with submodules (at least in you case):
1) Your super_repo should also be a git repo.
2) The sub modules are treated like a different entities in git, meaning changes from module will not reflect in another. For example, a commit in my_sub_1 will not reflect in my_sub_2.
3) Git will start tracking the my_sub_3 as a separate project as soon as it is added as a submodule.
4) "git status" after you added a submodule should show you .gitmodules, you will notice all the submodules inside the .gitmodules.
5) Here is the interesting part about git submodule, Lets say you did some changes in my_sub_3, Git sees it as a submodule and doesn’t track its contents when you’re not in that directory.
So if you are out of the directory the changes will not be tracked. If you commit at this stage, it will considered as a part of super_repo not the my_sub_3
Finally,
Submodules are not to be confused with remotes, which are other repositories of the same project; submodules are meant for different projects you would like to make part of your source tree, while the history of the two projects still stays completely independent and you cannot modify the contents of the submodule from within the main project.
You may want to add a remote for the other project and use the subtree merge strategy, instead of treating the other project as a submodule
Upvotes: 0
Reputation: 1324537
You could slightly change your process and:
git remote add origin URL
), you would first push that new repo to its remote destinationgit submodule add URL
)That way, you are sure the submodule is correctly initialized, with its .git located at the right spot.
Upvotes: 3