Daevin Daevin - 1 year ago 92
Git Question

Clone multiple repositories into common parent folder structure

My issue is that I have a client, let's call them 'Evil Corp', that provides my company with lots of business through various projects, which we use GitHub repos for. Let's take 2 and call them 'TotallyNotPolluting' and 'CoveringUpPolluting'.

I would like for my employees to be able to clone the 'TotallyNotPolluting' repo into a folder 'EvilCorp' by default when cloning.


So the starting structure is:


  1. Have starting folder structure:
    to start

  2. In Git Bash/GitHub Desktop/whatever clone the repo 'TotallyNotPolluting' (selected location is the starting folder structure

  3. Have ending folder structure:

  4. In Git Bash/GitHub Desktop/whatever clone the repo 'CoveringUpPolluting' (selected location is the starting folder structure

  5. Have ending folder structure:

So the final structure is:


This way, all the repos are organized into client folders without employees taking extra steps to do so.

Is this possible? To have the Clone action create then clone into (if one doesn't exist) or clone into (if one does) a parent directory?

NOTE: I have looked into using Submodules, but I don't want a repository "EvilCorp" that contains other repositories. I just want the folder when cloning.

Answer Source

You can write a script like below. I've not tested it properly but this should give you the right direction to move it. The purpose of this shell script is to create a parent directory and then individually create each sub-folder, check if sub-folder is empty. If empty, clone the repo. This shell script can be distributed to employees as an executable then, when run, will clone repos into right places.

#create the directory, if it does not exist

directoryName="<your directory name here>"
mkdir -p directoryName
repos="TotallyNotPolluting CoveringUpPolluting"
#go into directory
pushd $directoryName

for repo in $repos; do

    #check if directory is not present, if true, create the directory

    [ ! -d $repo ] && mkdir -p $repo

    #go into directory

    if [ "$(ls -A $currentDirectory)" ]; then

     echo "directory is not empty, skipping to clone"
        pushd $directoryName$forwardSlash$repo
        git clone "git@$codeHost:$codeUser/$repo.git"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download