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 git@github.com: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?
For orgs you have access to with private repos:
It uses the
html_url
, so you don't need anaccess_token
just enter your github password when prompted.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
Save that in a file,
chmod u+x
the file, then run it.Thanks to Arnaud for the ruby code.
This gist accomplishes the task in one line on the command line:
Replace
[your_org]
with your organization's name. And set yourper_page
if necessary.UPDATE:
As ATutorMe mentioned, the maximum page size is 100, according to the GitHub docs.
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.Note: The default
per_page
parameter is30
.So, in practice, if you want to clone all repos from the organization
FOO
which matchBAR
, you could use the one-liner below, which requires jq and common cli utilitiesClone all your repos that are not forks:
Clone your gists:
This
jq
command is complex because gists' repo's name are hashes, so that command concatenates all filenames to be the repo's nameYou can filter the JSON arbitrarily using
jq
install:
sudo apt-get install jq
In the example above, I filtered out forks using this:
curl ... | jq -r 'map(select(.fork == false))' ...
-- useful for not cloning repos where you've made casual pull requestsjq supports some very advanced features.
man jq
is your friendYou can authenticate with
curl -u "username" ...
to access private reposGuthub's API urls
https://api.github.com/user/repos\?page\=1\&per_page\=100
https://api.github.com/users/other_username/repos\?page\=1\&per_page\=100
https://api.github.com/orgs/orgname/repos\?page\=1\&per_page\=100
Github API Docs for repos
You can get a list of the repositories by using
curl
and then iterate over said list with a bash loop: