Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clone git submodules as separate directory inside workspace directory

Tags:

git

shell

I have a git repo (abc) which has two submodules and these submodules inturn as one submodule. I need to write a shell script wherein the only the url of the repo(abc) will be given as input to clone in the workspace directory. This I was able to do using git clone <url> --recursive command. But what I also want my shell script to do is clone all the submodule repo's recursively as well inside workspace directory (not inside cloned abc repo directory as subdirectories). Can this be done using shell script? In the end the workspace directory should contain 4 subdirectories (abc, submod1, submod2, common_submod).

like image 993
Harry Avatar asked Jun 27 '26 01:06

Harry


1 Answers

First clone the top-level repo to abc. Then run a loop over a list of desired submodules and clone each one recursively:

#! /bin/sh
set -e

git clone --recursive <URL> abc

for sm_path_key in `git -C abc config --file .gitmodules --name-only --get-regexp path`; do
    sm_path=`git -C abc config --file .gitmodules "$sm_path_key"`
    git clone --recursive abc/"$sm_path"
done

PS. Tested on my real repositories.

like image 128
phd Avatar answered Jun 29 '26 13:06

phd