15

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!

John Rumpel
  • 4,535
  • 5
  • 34
  • 48

2 Answers2

10

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.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
  • Thanks, this worked just fine! I later followed the John Douthat's answer here (http://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule) to remove the submodule from the original repo. – pedrovgp Jun 28 '16 at 17:34
7

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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 6
    I don't see an understandable reason for that. I want to reference my current projects state to a state of an extern repo. I don't need it within my repository. Anyways. Probably it will be supported in the future – John Rumpel Feb 26 '13 at 10:53
  • @JohnRumpel: Probably not. git only writes inside its repository's folder, so it wouldn't be able to create that submodule on a clone. – Daniel Hilgarth Feb 26 '13 at 11:05
  • 2
    So I have to write my own fork. flexibleGit ;-) – John Rumpel Feb 26 '13 at 11:37
  • 7
    It does seem to preclude using submodules for including Dependency1, Dependency2 and Dependency3 when Dependency1 and 2 both already have Dependency3 as a submodule themselves... – Roman Starkov Mar 31 '15 at 16:45
  • Is it such an odd thing to have a git repository that is used as submodule in *multiple* repositories? Somehow I'm the first person in history to want this. I have a library cloned from github. I need to use it in several projects. I have only two choices: add it as submodule to each one, cloning multiple copies of the exact same code in different places on my computer, or keep it outside, and be unable to use it as submodule. – Chuckk Hubbard Feb 05 '23 at 23:18
  • It blows my mind that anyone would ever use a submodule, thus obligating anyone who clones their repository to keep multiple copies of that submodule, if they use it for anything else. Fantastically bad design idea. – Chuckk Hubbard Feb 05 '23 at 23:19