Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 311
John Rumpel Avatar asked Feb 26 '13 10:02

John Rumpel


People also ask

How do I move a submodule to a different directory?

Move the submodule to its new home. Edit the config file, updating the worktree path so that it points to the new location of the submodule's working directory. Edit the . gitmodules file in the root of the master repository, updating the path to the working directory of the submodule.

How do I copy a submodule to a repo?

The list of steps required to clone a Git repository with submodules is: Issue a git clone command on the parent repository. Issue a git submodule init command. Issue a git submodule update command.

Does git pull pull submodules?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


2 Answers

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.

like image 128
Andrew Mao Avatar answered Sep 27 '22 18:09

Andrew Mao


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.

like image 43
Daniel Hilgarth Avatar answered Sep 27 '22 16:09

Daniel Hilgarth