简体   繁体   中英

Git submodule: Move submodule outside repository

after creating a git submodule by typing

git submodule add <repo> && git submodule init

my .git/config is changed, as well as a new file .gitmodules is created. It seems that I would be able to move my submodule to a specific folder within, as well as outside the repository:

$ cat gitmodules

[submodule "sub_repo"]
path = sub_repo
url = <...>

But when I try to move my repository to my parent folder and change my .gitmodules

path = ../sub_repo

it seems to ignore that module on 'git update' or 'git submodule foreach'.

What is my error in reasoning here?

Thanks a lot!

It's simply not supported, that's all. The whole point of submodules is to basically have one repository in another one.

If you don't want to do this, don't use submodules. Simply clone that other repository.

Moving a submodule out of its parent repo into a standalone directory simply involves updating some of the metadata. With Git 2.4, the steps are as follows:

  1. Starting from the repo, assuming the submodule is in my-submodule/ :

     mv my-submodule ../ 
  2. Remove the pointer .git file in the submodule (note that it isn't a directory):

     rm ../my-submodule/.git 
  3. Move the real git metadata for the submodule into the submodule's git repository:

     mv .git/modules/my-submodule/ ../my-submodule/.git 
  4. Edit the .git/config file by removing the worktree argument, which previously pointed to a relative path inside the repo.

That's it! At this point, your parent repo will show the submodule as deleted , while the submodule itself will work as normal (possible with a detached HEAD, which you can reset to master).

It's also possible to edit the .gitmodules file in the parent repo, but I've found that not doing this doesn't break anything, as long as you commit the deleted submodule.

If anyone else knows a faster way to do this (such as an actual git command), I'd be all ears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM