How to clone all repos at once from GitHub?
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:
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?
On Windows and all UNIX/LINUX systems, using Git Bash or any other Terminal, replace YOURUSERNAME
by your username and use:
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
andNAME=yourusername
, to download all your repositories. - Set
CNTX=orgs
andNAME=yourorgname
, to download all repositories of your organization.
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).
Here is a shell script that does the above: https://gist.github.com/erdincay/4f1d2e092c50e78ae1ffa39d13fa404e
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.
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.
It's a little bit of extra work, but it's necessary for GitHub to have proper authentication.
Organisation repositories
To clone all repos from your organisation, try the following shell one-liner:
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:
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:
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
):
# Usage: gh-clone-user (user)
gh-clone-user() {
curl -sL "https://api.github.com/users/$1/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 token either in your header like:
-H 'Authorization: token <token>'
or pass it in the param (?access_token=TOKEN
), for example:
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.
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.