Restore git submodules from .gitmodules

git submodule init only considers submodules that already are in the index (i.e. “staged”) for initialization. I would write a short script that parses .gitmodules, and for each url and path pair runs:

git submodule add <url> <path>

For example, you could use the following script:

#!/bin/sh

set -e

git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
    while read path_key local_path
    do
        url_key=$(echo $path_key | sed 's/\.path/.url/')
        url=$(git config -f .gitmodules --get "$url_key")
        git submodule add $url $local_path
    done

This is based on how the git-submodule.sh script itself parses the .gitmodules file.

Leave a Comment