Search code examples
gitmovegit-submodules

Moving submodules with Git


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


Solution

  • 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.