git 如何从 GitHub 一次性克隆所有存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19576742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clone all repos at once from GitHub?
提问by numb3rs1x
I have a company GitHub account and I want to back up all of the repositories within, accounting for anything new that might get created for purposes of automation. I was hoping something like this:
我有一个公司 GitHub 帐户,我想备份其中的所有存储库,考虑可能为自动化目的而创建的任何新内容。我希望是这样的:
git clone [email protected]:company/*.git
or similar would work, but it doesn't seem to like the wildcard there.
或类似的工作,但它似乎不喜欢那里的通配符。
Is there a way in Git to clone and then pull everything assuming one has the appropriate permissions?
假设一个人拥有适当的权限,有没有办法在 Git 中克隆然后提取所有内容?
采纳答案by Thomas Kelley
I don't think it's possible to do it that way. Your best bet is to find and loop through a list of an Organization's repositories using the API.
我认为不可能这样做。最好的办法是使用 API 查找和循环访问组织的存储库列表。
Try this:
尝试这个:
- Create an API token by going to Account Settings -> Applications
- Make a call to:
http://${GITHUB_BASE_URL}/api/v3/orgs/${ORG_NAME}/repos?access_token=${ACCESS_TOKEN}
- The response will be a JSON array of objects. Each object will include information about one of the repositories under that Organization. I think in your case, you'll be looking specifically for the
ssh_url
property. - Then
git clone
each of thosessh_url
s.
- 通过转到帐户设置-> 应用程序来创建 API 令牌
- 致电:
http://${GITHUB_BASE_URL}/api/v3/orgs/${ORG_NAME}/repos?access_token=${ACCESS_TOKEN}
- 响应将是一个 JSON 对象数组。每个对象都将包含有关该组织下的存储库之一的信息。我认为在您的情况下,您将专门寻找该
ssh_url
物业。 - 然后
git clone
每个ssh_url
s。
It's a little bit of extra work, but it's necessary for GitHub to have proper authentication.
这是一些额外的工作,但 GitHub 需要进行适当的身份验证。
回答by Erdinc Ay
On Windowsand all UNIX/LINUXsystems, using Git Bashor any other Terminal, replace YOURUSERNAME
by your username and use:
在Windows和所有UNIX/LINUX系统上,使用Git Bash或任何其他终端,替换YOURUSERNAME
为您的用户名并使用:
CNTX={users|orgs}; NAME={username|orgname}; PAGE=1
curl "https://api.github.com/$CNTX/$NAME/repos?page=$PAGE&per_page=100" |
grep -e 'git_url*' |
cut -d \" -f 4 |
xargs -L1 git clone
Set CNTX=users
and NAME=yourusername
, to download all your repositories. Set CNTX=orgs
and NAME=yourorgname
, to download all repositories of your organization.
设置CNTX=users
和NAME=yourusername
, 以下载所有存储库。设置CNTX=orgs
和NAME=yourorgname
, 以下载您组织的所有存储库。
The maximum page-size is 100, so you have to call this several times with the right page number to get all your repositories (set PAGE
to the desired page number you want to download).
最大页面大小为 100,因此您必须使用正确的页码多次调用它以获取所有存储库(设置PAGE
为您想要下载的所需页码)。
Here is a shell script that does the above: https://gist.github.com/erdincay/4f1d2e092c50e78ae1ffa39d13fa404e
这是执行上述操作的 shell 脚本:https: //gist.github.com/erdincay/4f1d2e092c50e78ae1ffa39d13fa404e
回答by kenorb
Organisation repositories
组织存储库
To clone all repos from your organisation, try the following shell one-liner:
要从您的组织克隆所有存储库,请尝试以下 shell 单行:
GHORG=company; curl "https://api.github.com/orgs/$GHORG/repos?per_page=1000" | grep -o 'git@[^"]*' | xargs -L1 git clone
User repositories
用户存储库
Cloning all using Git repository URLs:
使用 Git 存储库 URL 克隆所有内容:
GHUSER=CHANGEME; curl "https://api.github.com/users/$GHUSER/repos?per_page=1000" | grep -o 'git@[^"]*' | xargs -L1 git clone
Cloning all using Clone URL:
使用克隆 URL 克隆所有内容:
GHUSER=CHANGEME; curl "https://api.github.com/users/$GHUSER/repos?per_page=1000" | grep -w clone_url | grep -o '[^"]\+://.\+.git' | xargs -L1 git clone
Here is the useful shell function which can be added to user's startup files (using curl
+ jq
):
这是可以添加到用户启动文件中的有用的 shell 函数(使用curl
+ jq
):
# Usage: gh-clone-user (user)
gh-clone-user() {
curl -sL "https://api.github.com/users//repos?per_page=1000" | jq -r '.[]|.clone_url' | xargs -L1 git clone
}
Private repositories
私有仓库
If you need to clone the private repos, you can add Authorization tokeneither in your header like:
如果您需要克隆私有存储库,您可以在标头中添加授权令牌,例如:
-H 'Authorization: token <token>'
or pass it in the param (?access_token=TOKEN
), for example:
或在参数 ( ?access_token=TOKEN
) 中传递它,例如:
curl -s "https://api.github.com/users/$GHUSER/repos?access_token=$GITHUB_API_TOKEN&per_page=1000" | grep -w clone_url | grep -o '[^"]\+://.\+.git' | xargs -L1 git clone
Notes:
笔记:
- To fetch only private repositories, add
type=private
into your query string. - Another way is to use
hub
after configuring your API key.
- 要仅获取私有存储库,请添加
type=private
到您的查询字符串中。 - 另一种方法是
hub
在配置您的 API 密钥后使用。
See also:
也可以看看:
- GitHub REST API v3 - List your repositories
- How to download GitHub Release from private repo using command line.
- How to retrieve the list of all github repositories of a person?.
Hints:
- To increase speed, set number of parallel processes by specifying -P
parameter for xargs
(-P4
= 4 processes).
- If you need to raise the GitHub limits, try authenticating by specifying your API key.
- Add --recursive
to recurse into the registered submodules, and update any nested submodules within.
提示:
- 要提高速度,请通过-P
为xargs
( -P4
= 4 个进程)指定参数来设置并行进程数。
- 如果您需要提高 GitHub 限制,请尝试通过指定 API 密钥进行身份验证。
- 添加--recursive
递归到注册的子模块,并更新其中的任何嵌套子模块。
回答by seancdavis
This gistaccomplishes the task in one line on the command line:
这个要点在命令行的一行中完成了任务:
curl -s https://api.github.com/orgs/[your_org]/repos?per_page=200 | ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
Replace [your_org]
with your organization's name. And set your per_page
if necessary.
替换[your_org]
为您组织的名称。并per_page
在必要时设置您的。
UPDATE:
更新:
As ATutorMe mentioned, the maximum page size is 100, according to the GitHub docs.
正如 ATutorMe 所提到的,根据 GitHub 文档,最大页面大小为 100 。
If you have more than 100 repos, you'll have to add a page
parameter to your url and you can run the command for each page.
如果您有 100 个以上的存储库,则必须page
向您的 url添加一个参数,并且您可以为每个页面运行该命令。
curl -s "https://api.github.com/orgs/[your_org]/repos?page=2&per_page=100" | ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
Note: The default per_page
parameter is 30
.
注意:默认per_page
参数是30
.
回答by Cory Klein
Go to Account Settings -> Application and create an API key
Then insert the API key, github instance url, and organization name in the script below
转到帐户设置 -> 应用程序并创建一个 API 密钥
然后在下面的脚本中插入 API 密钥、github 实例 url 和组织名称
#!/bin/bash
# Substitute variables here
ORG_NAME="<ORG NAME>"
ACCESS_TOKEN="<API KEY>"
GITHUB_INSTANCE="<GITHUB INSTANCE>
URL="https://${GITHUB_INSTANCE}/api/v3/orgs/${ORG_NAME}/repos?access_token=${ACCESS_TOKEN}"
curl ${URL} | ruby -rjson -e 'JSON.load(STDIN.read).each {|repo| %x[git clone #{repo["ssh_url"]} ]}'
Save that in a file, chmod u+x
the file, then run it.
将其保存在一个文件中,chmod u+x
该文件,然后运行它。
Thanks to Arnaudfor the ruby code.
感谢Arnaud提供的 ruby 代码。
回答by Kirby
So, I will add my answer too. :) (I found it's simple)
所以,我也会添加我的答案。:)(我发现这很简单)
Fetch list (I've used "magento" company):
获取列表(我使用过“magento”公司):
curl -si https://api.github.com/users/magento/repos | grep ssh_url | cut -d '"' -f4
Use clone_url
instead ssh_url
to use HTTP access.
使用clone_url
而不是ssh_url
使用HTTP访问。
So, let's clone them all! :)
所以,让我们克隆它们!:)
curl -si https://api.github.com/users/magento/repos | \
grep ssh_url | cut -d '"' -f4 | xargs -i git clone {}
If you are going to fetch private repo's - just add GET parameter ?access_token=YOURTOKEN
如果您要获取私有仓库 - 只需添加 GET 参数 ?access_token=YOURTOKEN
回答by Garren S
I found a comment in the gist@seancdavis provided to be very helpful, especially because like the original poster, I wanted to sync all the repos for quick access, however the vast majority of which were private.
我发现@seancdavis 提供的要点中的评论非常有帮助,尤其是因为像原始海报一样,我想同步所有存储库以便快速访问,但其中绝大多数是私有的。
curl -u [[USERNAME]] -s https://api.github.com/orgs/[[ORGANIZATION]]/repos?per_page=200 |
ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
Replace [[USERNAME]] with your github username and [[ORGANIZATION]] with your Github organization. The output (JSON repo metadata) will be passed to a simple ruby script:
将 [[USERNAME]] 替换为您的 github 用户名,将 [[ORGANIZATION]] 替换为您的 Github 组织。输出(JSON 存储库元数据)将传递给一个简单的 ruby 脚本:
# bring in the Ruby json library
require "json"
# read from STDIN, parse into ruby Hash and iterate over each repo
JSON.load(STDIN.read).each do |repo|
# run a system command (re: "%x") of the style "git clone <ssh_url>"
%x[git clone #{repo["ssh_url"]} ]
end
回答by muhasturk
I made a script with Python3 and Github APIv3
我用 Python3 和 Github APIv3 做了一个脚本
https://github.com/muhasturk/gitim
https://github.com/muhasturk/gitim
Just run
赶紧跑
./gitim
回答by Evren Ozkan
This python one-liner will do what you need. It:
这个 python one-liner 将满足您的需求。它:
- checks github for your available repos
for each, makes a system call to
git clone
python -c "import json, urllib, os; [os.system('git clone ' + r['ssh_url']) for r in json.load(urllib.urlopen('https://api.github.com/orgs/<<ORG_NAME>>/repos?per_page=200'))]"
- 检查 github 以获取您的可用存储库
对于每个,进行系统调用
git clone
python -c "import json, urllib, os; [os.system('git clone ' + r['ssh_url']) for r in json.load(urllib.urlopen('https://api.github.com/orgs/<<ORG_NAME>>/repos?per_page=200'))]"
回答by julianzhang
curl -s https://api.github.com/orgs/[GITHUBORG_NAME]/repos | grep clone_url | awk -F '":' '{ print }' | sed 's/\"//g' | sed 's/,//' | while read line; do git clone "$line"; done