9

Is there any way to move submodules within your superproject without removing them first and re-adding them ?

3
  • This was asked in 2010. The question you linked is from 2011
    – rafi
    Apr 30, 2012 at 10:55
  • Sure, but the other question is also much more “popular” (e.g. it has over three times the views; it and its answers have garnered many more votes–even the accepted, identical answer). Anyway, it does not matter much since any close votes expired long ago. Apr 30, 2012 at 22:12
  • This question was also asked here: stackoverflow.com/questions/4604486/… May 2, 2012 at 9:48

2 Answers 2

5

It's similar to how you removing a submodule (see How do I remove a submodule?):

  • Edit .gitmodules and change the path of the submodule appropriately, and put it in the index with git add .gitmodules
  • If needed, create the parent directory of the new location of the submodule: mkdir -p new/parent
  • Move all content from the old to the new directory: mv -vi old/parent/submodule new/parent/submodule
  • Remove the old directory with git rm --cached old/parent/submodule

Looks like this for me afterwards:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   .gitmodules
#       renamed:    old/parent/submodule -> new/parent/submodule
#
  • Finally commit the changes.
4
  • 4
    Wish there was a Git command that could all of that for you ;)
    – rafi
    Jun 23, 2011 at 18:15
  • 1
    These instructions don't work.
    – awolf
    Oct 10, 2011 at 18:11
  • 3
    Didn't work for me either; new/parent/submodule showed up as an untracked file, old/parent/submodule as deleted. I ended up removing & readding the submodule
    – wutz
    Nov 7, 2011 at 11:48
  • 1
    You need to git add the new submodule directory.
    – mpe
    Dec 20, 2011 at 2:54
1

I don't think there is for now.

There is a patch in the making for a teaching "git mv" how to handle moving submodules, including how to update the .gitmodules file.
But it is not there yet.
Simply switching a remote repo for an existing submodule is simpler, but not what you want.

Not the answer you're looking for? Browse other questions tagged or ask your own question.