Ever wanted to use a previous project to seed a new one?

We often have to do that. With re-using an existing project, we typically need to see our git repo with also the submodules that were used.

Here is a script that does just that!

#!/bin/sh
# credit: https://stackoverflow.com/questions/11258737/restore-git-submodules-from-gitmodules
set -e

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

Simply run it bash ./shell/restore-git-submodules-from-gitmodules.sh and all the submodules should be in the new repo!