Retrieving git submodules without a .git directory

This comes in handy when you have a copy of a git repository without the .git metadata directory included, for instance a .zip export from Bitbucket.

For use in a shell script.

if [ ! -d '.git' ]; then
    git init .

    # Reconstruct submodules using .gitmodules
    # Based on https://stackoverflow.com/a/11258810
    git config -f .gitmodules --get-regexp '^submodule\..*\.path
    while read path_key path
        rm -rf $path
        url_key=$(echo $path_key | sed 's/\.path/.url/')
        branch_key=$(echo $path_key | sed 's/\.path/.branch/')
        url=$(git config -f .gitmodules --get "$url_key")
        branch=$(git config -f .gitmodules --get "$branch_key")
        git submodule add -b $branch --depth 1 $url $path

# Continue as we would /with/ a .git directory
git submodule foreach --recursive 'git fetch --tags'
git submodule update --recursive --init

Tags: , | Posted in Uncategorized