Run:
$ git mv old/path/to/module new/path/to/module
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/path/to
).
Move all content from the old to the new directory (mv -vi old/path/to/module new/path/to/submodule
).
Make sure Git tracks this directory (git add new/path/to
).
Remove the old directory with git rm --cached old/path/to/module
.
Move the directory .git/modules/old/path/to/module
with all its content to .git/modules/new/path/to/module
.
Edit the .git/modules/new/path/to/config
file, make sure that worktree item points to the new locations, so in this example it should be worktree = ../../../../../old/path/to/module
. Typically there should be two more ..
then directories in the direct path in that place.
. Edit the file new/path/to/module/.git
, make sure that the path in it points to the correct new location inside the main project .git
folder, so in this example gitdir: ../../../.git/modules/new/path/to/module
.
git status
output looks like this afterwards:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: .gitmodules
# renamed: old/path/to/submodule -> new/path/to/submodule
#
Finally, commit the changes.
This example from Stack Overflow, by Axel Beckert