是否可以git clone
使用一个命令创建多个 git 存储库(例如:git clone "1.git,2.git,3.git.."
在一个本地目录中?
14 回答
这就是我自己几乎解决的方法:
git clone https://myrepo.com/folder.git && \
git clone https://myrepo.com/folder2.git && \
git clone https://myrepo.com/folder3.git
使用像 Sublime 或 VSCode 这样的代码编辑器更容易构建。
对我来说唯一的缺点:如果你没有存储你的凭据,你将不得不一遍又一遍地输入它。
您可以找到这样的脚本示例:
我有一个名为“克隆”的文件,其中包含几个 git 存储库的 URL(取自djangosites.com。很棒的网站。必须访问)
片段:
$ cat clone
https://github.com/igorsobreira/igorsobreira.com https://github.com/ella/ella https://github.com/divio/django-cms/ https://github.com/palewire/palewire.com https://github.com/jabapyth/jfcom https://github.com/humanfromearth/snippify https://github.com/scaphilo/koalixcrm https://github.com/jlev/Boycott-Toolkit https://github.com/jbalogh/zamboni/ https://github.com/ASKBOT/askbot-devel https://github.com/emesik/djiki https://github.com/vicalloy/LBForum https://github.com/agiliq/agiliq https://github.com/bartTC/dpaste.de https://github.com/bartTC/django-paste https://github.com/bartTC/dpaste_de/ https://github.com/fotochest/fotochest https://esp.mit.edu/git/esp-project.git https://github.com/titan2x/bashoneliners.git
显然,一次克隆多个存储库更难(
git clone <repo1> <repo2> ... <repon>
不起作用)。所以我写了这个简短的 bash 代码来使它工作:代码:
atm in /home/atm/git/django_repos
$ for f in `cat clone`; do `git clone $f`; done
你会在gist.github.com上找到更多,比如这个,从 GitHub 克隆你所有的 repos:
#!/bin/bash
#
# Copyright 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
# Dual licensed under the MIT and GPL licenses.
#
# Automatically clone single or multiple repos into a folder,
# great for setting up a git projects folder.
#
# Install: curl https://gist.github.com/raw/902154/github.sh > /usr/local/bin/gh
# chmod +x /usr/local/bin/gh
#
# Internal properties
GITHUB_PREFIX=git@github.com:
GITHUB_USERNAME=$(git config --global github.user)
function main {
# Improperly configured user
detect_user
# Missing arguments
args=$1
if [ -z $args ]; then
echo '
gh: try ''`gh --help`'' for more information
'
exit
fi
# Display help text
if [ $args = '--help' ]; then
echo '
Clone repos from your GitHub
gh repo1 repo2
Clone repos from others GitHub
gh username/repo1 username/repo2
Clone mixed repos:
gh repo1 username/repo2
Clone line separated repos from file:
cat file | xargs gh
'
exit
fi
# Parse arguments and clone repos.
find_repos
}
function detect_user {
# If no username configured, attempt to pull from git --config
if [ -n "$GITHUB_USERNAME" ]; then
USERNAME=$GITHUB_USERNAME
else
echo '
gh: missing username
configure username with ''`git config --global github.user username`''
'
exit
fi
}
function find_repos {
for repo in $args; do
# If a user provides the parameter username/repo pull in that specific repository.
if [ `awk -v repo="$repo" -v delimit="/" 'BEGIN{print index(repo,delimit)}'` -ne 0 ]; then
echo "Pulling in $repo";
git clone $GITHUB_PREFIX$repo.git
# Default to you.
else
echo "Pulling in $USERNAME/$repo";
git clone $GITHUB_PREFIX$USERNAME/$repo.git
fi
done
}
main $*
更一般地说,需要一种脚本方法,并lilgeek
提到bl4ckbo7/agt,这是一个 python 脚本,其中包括具有最快和并行克隆处理功能的克隆。
如果所有存储库都托管在同一命名空间(用户名)下,您可以执行以下操作:
$ echo name1 name2 name3 | xargs -n1 | xargs -I{} git clone https://github.com/username/{}
解释
第一部分将用空格分隔的名称分成多行
$ echo name1 name2 name3 | xargs -n1 name1 name2 name3
然后,这些名称中的每一个都单独传递给下一个
xargs
调用,该调用调用git clone
并用存储库名称替换{}
URL 中的子字符串,这基本上转换为$ git clone https://github.com/username/name1 $ git clone https://github.com/username/name2 $ git clone https://github.com/username/name3
如果并非所有 repos 都托管在同一个命名空间下,您可以将动态部分移动到echo
部分,并将 URL 的公共部分保留在最后一部分中。
如果你使用windows,你可以使用powershell。
只需在喜欢的文本编辑器中创建列表,如下所示:
git clone https://github.com/1.git;
git clone https://github.com/2.git;
git clone https://github.com/3.git;
git clone https://github.com/4.git;
git clone https://github.com/5.git;
在 powershell 控制台中执行您的目录。
例子
cd d:/myGitRepos
然后直接复制repo list做powershell控制台。第一次它会询问用户名和密码,但之后它会记住它。
您可以使用混合解决方案。
使用 git 的 credential.helper 设置缓存超时(请参阅this),然后您可以使用Fábio建议的脚本/列表。这样,您只需键入一次凭据(通常,除非克隆时间超过缓存超时时间。
git config --global credential.helper 'cache timeout 3600'
# In some clients, the above line might only work with double quotes
# git config --global credential.helper "cache timeout 3600"
git clone https://myuser@bitbucket.org/myproject/myrepo.git
### password should be prompted
git clone https://myuser@bitbucket.org/myproject/mysecondrepo.git
### look ma, no password prompt!
git clone https://myuser@bitbucket.org/myproject/mythirdrepo.git
git clone https://myuser@bitbucket.org/myproject/myfourthrepo.git
git clone https://myuser@bitbucket.org/myproject/andsoforthrepo.git
恕我直言,这仍然是连续的,但它有帮助并且非常简单。
我为我的项目创建了一个示例脚本。我们的项目包括许多需要在新人加入团队时克隆的存储库。
所以这里是脚本的内容,您可以将其保存到一些可执行文件中并执行。
echo -n Please Enter your GitHub Username:
read username
echo -n Please Enter your GitHub Password
read -s password // doesn't echoes the password on screen
git clone
https://$username:$password@github.com/location/reponame1.git
https://$username:$password@github.com/location/reponame2.git
....
https://$username:$password@github.com/location/reponamen.git
它对于避免枯燥的手动工作并确保一次性克隆所有需要的项目非常有用。
希望我的回答也对其他人有所帮助。
嗨,假设您想通过 ssh 而不是 https 执行此操作,并且您已经设置了 ssh 密钥。此外,您可能在同一个组织中有多个存储库,您可以在 bash 中使用 for。
BASE='git clone git@github.com:/myrepo.com/'
while IFS= read -r THELINE; do
$BASE"$THELINE"
done </PATHTOLISTFILE
确保在列表中你有一个包含所有 repos 的文件的路径,基本上你得到:
git clone git@github.com:/myrepo.com/folder1
git clone git@github.com:/myrepo.com/folder2
我有同样的问题,我写了这个简单的 bash 来做
#!/bin/bash
#get repo number
read -p "how many repo you want to clone:" reponum
#take username&password
read -p "enter git username: " username
read -p "enter git password: " password
#urlencode password to use it in https without any issues
epassword=$(php -r "echo urlencode('$password');")
while ((reponum--))
do
#take repo name without .git at the end
read -p "enter repo name: " reponame
git clone "https://$username:$epassword@github.com/$username/$reponame.git"
done
我发现这个对我有用的 PowerShell 脚本:
# import multiple remote git repositories to local Source dir
param (
[string]$localFolder = "C:\repos\",
[array]$repos = @("repo1", "repo2")
)
# change the repoLocation based on your github access
$repoLocation = "https://github.com/"
$gitExtension = ".git"
# for each repo found remotely, check if it exists locally
# if dir exists, skip, if not, clone the remote git repo into it
foreach ($gitRepo in $repos) {
If (Test-Path $localFolder\$gitRepo) {
echo "repo $gitRepo already exists"
}
Else {
echo "git clone $repoLocation$gitRepo$gitExtension $localFolder\$gitRepo"
git clone $repoLocation$gitRepo$gitExtension $localFolder\$gitRepo
}
}
在阅读了这篇文章和其他文章后,我以这个脚本结束了。
我需要在本地环境中对我的存储库进行身份验证。
因此,脚本将询问您用户、密码和一个选项,以防您想在存储库中记住您的凭据。
出于安全原因,我从 git 的配置文件中删除用户和密码。(不确定这些凭据是否存储在其他地方)
#!/bin/bash
#
#This script is intended to be used with repositories that need authentication
#
#Funtion to erase the $username:$password@ from the config file from git in the repo
function erase_user_pass() {
printf "${green}lets clean $1 from $2 \n${normal}"
sed -i 's/'"$1"'/'""'/g' $2
}
#function to download the repo
#1-clone the repo
#2-fix the config file by deleting the user and password
#3-execute the git command to automatically store the credentials if the user answer yes on the startup
#
# param 1 = name of the repo
# param 2 = string to complet the url of the repo
#
# example repo in https://git.something.com/yourcompany/your.project.git
#
# the param1 should be your.project
# the param2 should be git.something.com/yourcompany/your.project.git (without 'https://')
#
# download_repo 'your.project' 'git.something.com/yourcompany/your.project.git'
#
function download_repo() {
path=$(pwd)
printf "${blue}\n Importing $1\n\n${normal}"
git clone https://$username:$password@$2
file="$(pwd)/$1/.git/config"
replace_string="$username:$password@"
erase_user_pass $replace_string $file
cd ./$1
if [ "$STORE_OK" == 'Yes' ]
then
printf "${green} store credentials for the repo $1 \n${normal}"
git config credential.helper store
fi
cd $path
}
blue=$(tput setaf 4)
green=$(tput setaf 2)
normal=$(tput sgr0)
echo -n Please Enter your GitHub Username:
read username
echo -n Please Enter your GitHub Password:
read -s password
printf "\n\nDo you want to store your credentials locally?:"
STORE_OK=
select STORE_OK in Yes No
do
case $STORE_OK in
'') echo 'Please enter 1 for Yes, or 2 for No.';;
?*) break
esac
done
printf "${blue}\n\n lets import your repos\n\n${normal}"
# repo 1 https://git.something.com/yourcompany/your.project.git
download_repo 'your.project' 'git.something.com/yourcompany/your.project.git'
# repo 2 https://git.something.com/yourcompany/your.project2.git
download_repo 'your.project2' 'git.something.com/yourcompany/your.project2.git'
printf "${blue}\n Enjoy :)"
exit 0
您应该需要替换对 download_repo 的调用以指向您的存储库,这些都是假的。
问候并感谢其他人的回答。
通过 SSH 进行多个 repo 克隆:
注意:只有授权用户才能使用此脚本。
#!/bin/bash
#get repo count
read -p "how many repo you want to clone:" repo_count
while [ $repo_count -gt 0 ]
do
#take repo name without .git at the end
read -p "enter repo name: " reponame
git clone "Enter your base URL here/$reponame.git"
repo_count=$(( $repo_count - 1 ))
done
将存储库一一克隆到同一目录中:
xargs -L1 git clone <<EOF
https://github.com/motdotla/dotenv.git
https://github.com/node-fetch/node-fetch.git
EOF
这是一个简单的cmd
版本:
@echo off
SET repos=1.git,2.git,3.git,4.git
SET baseUrl=https://BaseUrl.Company.Com/Project/Whatever/
FOR %%r IN (%repos%) DO (
git clone %baseUrl%%%r
)
SET /p username="Username : "
或者,您还可以在您的 bat 文件中设置用户名/密码,或者使用等请求用户输入。
如果您要从一个目录克隆多个存储库,这是一个很好的解决方案
eval $(printf "git clone \"%s\" & " <git_repo_directory>/*)
where<git_repo_directory>
必须替换为包含 git 存储库的路径