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 mvcommand, right in the question.git mvlike this. Usedeinitthenrmas specified stackoverflow.com/a/18892438/8047.git mvjust works for submodules also, no need for anything else.1.8.5moving submodules is supported natively using thegit mvcommand (from the release notes, first linked by @thisch himself). Also answered heregit mvdoes 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)