Convert git repo to submodule And move the .git directory to superepo

I have a super repo and a few submodules in this repo:

MY_SUPER_REPO

  • my_sub_1
  • my_sub_2

Now I create a new directory, which will later become a submodule:

  • my_dir_3

When I am ready to become subrepot, I do:

git init git remote add origin URL 

Then I go to super repo and

 git submodule add URL 

Now. gitmodules are updated accordingly, but the .git directory remains in my_dir_3 , and the hooks seem to be taken here when I work in this subunit, and 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, hooks are not used in the super-repo.

Is there any β€œmagic” command to fix this or do I need to copy .git manually?

+5
source share
2 answers

You can change your process a bit and:

  • After you have added the remote url ( git remote add origin URL ), you must first push this new repo to your remote destination
  • then you delete the folder
  • finally, you add your submodule in the usual way ( git submodule add URL )

Thus, you are sure that the submodule is correctly initialized, and its .git is in the right place.

+3
source

I answered based on what I understood from your question, comment if you need something.

Let's say you create a new directory, which will later become a submodule. If you do not want to track changes in this directory, add this to .gitignore of you super_repo. Now let me say that you decided to make it a submodule, do it from you super_repo.

1) Remove the directory from .gitignore

2) Initialize your my_dir_3

git init

3) Add a submodule to your super repo

git subodule add module_name

Your submodule is added to the super repo. Things you need to know to work with submodules (at least in your case):

1) Your super_repo should also be a git repository.

2) Submodules are processed as different objects in git, that is, changes from the module will not be reflected in another. For example, the commit in my_sub_1 will not appear in my_sub_2.

3) git will start tracking my_sub_3 as a separate project as soon as it is added as a submodule.

4) "git status" after you added the submodule, you should show you .gitmodules, you will notice all the submodules inside .gitmodules.

5) Here is the interesting part about the git submodule. Suppose you made some changes to my_sub_3, git sees the submodule in it and does not track its contents when you are not in this directory.

So, if you are outside the directory, changes will not be tracked. If you commit at this point, it will be considered as part of super_repo, not my_sub_3

Finally,

Submodules should not be confused with remotes, which are other repositories of the same project; submodules are designed for different projects that you would like to make part of your source tree, while the history of the two projects still remains completely independent, and you cannot modify the contents of the submodule from the main project.

You can add a remote for another project and use the subtree merge strategy , instead of considering the other project as a submodule

0
source

Source: https://habr.com/ru/post/1258086/


All Articles