Skip to content

Instantly share code, notes, and snippets.

@rkok
Last active November 21, 2018 15:40
Show Gist options
  • Save rkok/a0f3b90ea33bd766cf93305799e33471 to your computer and use it in GitHub Desktop.
Save rkok/a0f3b90ea33bd766cf93305799e33471 to your computer and use it in GitHub Desktop.
Retrieve 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
do
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
done
fi
# Continue as we would /with/ a .git directory
git submodule foreach --recursive 'git fetch --tags'
git submodule update --recursive --init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment