2

I'd like to clone multiple remote repositories that all have a similiar naming context. For example, they look this:

foo/stuff
foo/morestuff
foo/evenmorestuff

My question is, can I clone all of these repositories into separate directories on my local machine using something like:

git clone user@server:foo/*

Thanks in advance.

mxmxx
  • 219
  • 4
  • 8

4 Answers4

1

You could use some bash functionality to achieve this. You can retrieve all the repositories via ssh and find.

ssh user@server 'find foo/ -mindepth 1 -maxdepth 1 -type d'

-mindepth 1 and -maxdepth 1 ensure that you only get items directly under foo/ but not foo/ itself or any nested files. The -type d argument returns only directories.

Now you can iterate over all results and execute git clone for each of them.

for repo in $(ssh user@server 'find foo/ -mindepth 1 -maxdepth 1 -type d'); do
    git clone user@server:${repo}
done
hdlgi42
  • 74
  • 5
  • Thanks for the suggestion. I considered this but there's an access control layer that prevents true shell access to git server, so I'm unable to run find to query. – mxmxx May 12 '16 at 13:50
  • I see your problem, @mxmxx. Depending on where those repositories are located maybe you can retrieve the paths from the one running the servers as recommended in [this thread](http://stackoverflow.com/a/34183734/6314540). – hdlgi42 May 14 '16 at 15:11
1

If your repos are on Github you can use gh (https://cli.github.com/manual/installation) command line interface:

In your case this one-liner would do a job:

gh repo list foo --limit 99999 | while read -r repo _; do gh repo clone "$repo" $repo; done

In case if would like to clone only repos starting with a particular phrase:

gh repo list foo --limit 99999 | while read -r repo _; do if [[ $repo =~ "^(foo/stuff*)" ]]; then gh repo clone "$repo" $repo; fi; done
Przemek Nowicki
  • 572
  • 4
  • 7
0

If you already have the list of repository names, you can try the following solution in bash:

#!/bin/bash

# Generate a clean project list from your local Git Source directory you want to clone as fresh from your remote repository
ls | sed 's/\///g' > cleanListOfGitProjects.txt

# OPTION1: When remote repository path is static
#Loop through the clean list and append with your remote repository path for mulitple cloning of repositories
while read p; do echo git clone https://YOUR-REMOTE-GIT-PATH/$p; done < cleanListOfGitProjects.txt >> multipleGitCloneProjects.sh

# OPTION2: When remote repository path is different based on specific string within project names
#Loop through the clean list and append with your remote repository path for mulitple cloning of repositories which for example contain 'ansible-role' string
#while read p; do echo git clone https://YOUR-REMOTE-GIT-PATH/FOLDER_NAME/$p; done < cleanListOfGitProjects.txt | awk '/ansible-role-*/' >> multipleGitCloneProjects.sh

https://gitlab.com/lefthandedninja/clone-multiple-git-repositories

0

I recently did a helper to clone and act on multiple repositories with a single command using a configuration file. It's not perfect, but it does the job: https://github.com/headyj/giterate

theplayer777
  • 163
  • 1
  • 15