You can just add a new submodule and remove the old submodule using standard commands. (should prevent any accidental errors inside of .git)
Example setup:
mkdir foo; cd foo; git init;
echo "readme" > README.md; git add README.md; git commit -m "First"
## add submodule
git submodule add git://github.com/jquery/jquery.git
git commit -m "Added jquery"
## </setup example>
Examle move 'jquery' to 'vendor/jquery/jquery' :
oldPath="jquery"
newPath="vendor/jquery/jquery"
orginUrl=`git config --local --get submodule.${oldPath}.url`
## add new submodule
mkdir -p `dirname "${newPath}"`
git submodule add -- "${orginUrl}" "${newPath}"
## remove old submodule
git config -f .git/config --remove-section "submodule.${oldPath}"
git config -f .gitmodules --remove-section "submodule.${oldPath}"
git rm --cached "${oldPath}"
rm -rf "${oldPath}" ## remove old src
rm -rf ".git/modules/${oldPath}" ## cleanup gitdir (housekeeping)
## commit
git add .gitmodules
git commit -m "Renamed ${oldPath} to ${newPath}"
Bonus method for large submodules:
If the submodule is large and you prefer not to wait for the clone, you can create the new submodule using the old as origin, and then switch the origin.
Example (use same example setup)
oldPath="jquery"
newPath="vendor/jquery/jquery"
baseDir=`pwd`
orginUrl=`git config --local --get submodule.${oldPath}.url`
# add new submodule using old submodule as origin
mkdir -p `dirname "${newPath}"`
git submodule add -- "file://${baseDir}/${oldPath}" "${newPath}"
## change origin back to original
git config -f .gitmodules submodule."${newPath}".url "${orginUrl}"
git submodule sync -- "${newPath}"
## remove old submodule
...
git mv
command, right in the question. – Monostomegit mv
like this. Usedeinit
thenrm
as specified https://mcmap.net/q/22679/-how-do-i-move-an-existing-git-submodule-within-a-git-repository. – Monostomegit mv
just works for submodules also, no need for anything else. – Lanchow1.8.5
moving submodules is supported natively using thegit mv
command (from the release notes, first linked by @Freezing himself). Also answered here – Theirgit mv
does move the submodule in the workspace, and update the submodule .git files correctly, but the subfolder within the .git/modules folder of the parent repo stays the same - is that ok? (I'm using git 2.19.0 on Windows) – Billiebilling