Newer versions of git
Git now has native support for moving submodules:
Since git 1.8.5,
git mv old/submod new/submod
works as expected and does all the plumbing for you. You might want to use git 1.9.3 or newer, because it includes fixes for submodule moving.
Older versions of git
As mentioned in the comments, this answer refers to the steps needed with older versions of git.
The process is similar to how you’d remove 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 withgit 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
). -
Make sure Git tracks this directory (
git add new/parent
). -
Remove the old directory with
git rm --cached old/parent/submodule
. -
Move the directory
.git/modules/old/parent/submodule
with all its content to.git/modules/new/parent/submodule
. -
Edit the
.git/modules/new/parent/config
file, make sure that worktree item points to the new locations, so in this example it should beworktree = ../../../../../new/parent/module
. Typically there should be two more..
than directories in the direct path in that place. -
Edit the file
new/parent/module/.git
, make sure that the path in it points to the correct new location inside the main project.git
folder, so in this examplegitdir: ../../../.git/modules/new/parent/submodule
.git status
output 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.