Git克隆多个存储库

时间:2016-05-11 14:51:16

标签: git

我希望克隆多个具有类似命名上下文的远程存储库。例如,他们看起来像这样:

foo/stuff
foo/morestuff
foo/evenmorestuff

我的问题是,我可以使用以下内容将所有这些存储库克隆到本地计算机上的不同目录中:

git clone user@server:foo/*

提前致谢。

4 个答案:

答案 0 :(得分:1)

您可以使用一些bash功能来实现此目的。 您可以通过sshfind检索所有存储库。

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

-mindepth 1-maxdepth 1确保您只能直接在foo/但不是foo/本身或任何嵌套文件下获取项目。 -type d参数仅返回目录。

现在,您可以迭代所有结果并为每个结果执行git clone

for repo in $(ssh user@server 'find foo/ -mindepth 1 -maxdepth 1 -type d'); do
    git clone user@server:${repo}
done

答案 1 :(得分:0)

如果您已经有了存储库名称列表,则可以在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

答案 2 :(得分:0)

我最近做了一个帮助程序,使用配置文件通过单个命令克隆并在多个存储库上操作。它并不完美,但可以完成工作:https://github.com/headyj/giterate

答案 3 :(得分:0)

如果您的存储库在 Github 上,您可以使用 gh (https://cli.github.com/manual/installation) 命令行界面:

在你的情况下,这个单线可以完成工作:

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

如果只想克隆以特定短语开头的存储库:

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