Batch clone git repositories
Install gh to use github client.
# install with homebrew
brew install gh
# install with curl
curl -sS https://webi.sh/gh | sh
Login using the following command. This gives a permission to access your account.
gh auth login
Try some of the commands below to make sure gh is working:
gh repo list --limit 1000
gh repo list --limit 1000 --visibility public
gh repo list --limit 1000 --visibility private
gh repo list --limit 1000 --archived
gh repo list --limit 1000 --no-archived
We want to display all of our repositories’ SSH URL in JSON format.
gh repo list --json=sshUrl --limit=1000
The output should look something like the below.
[
{
"sshUrl": "git@github.com:img9417/minimalist-blog.git"
},
{
"sshUrl": "git@github.com:img9417/portfolio_v2.git"
},
{
"sshUrl": "git@github.com:img9417/tadanoyu.github.io.git"
},
...
]
But we actually want to display the sshUrl value only per line. We can use jq to parse the output.
# install jq
brew install jq
# use jq to parse the output
gh repo list --json=sshUrl --limit=1000 -q ".[].sshUrl"
Here’s the output.
git@github.com:img9417/minimalist-blog.git
git@github.com:img9417/portfolio_v2.git
git@github.com:img9417/img9417.github.io.git
Now we’re going to send these URL’s one by one using the pipe (|) and clone it.
gh repo list --json=sshUrl --limit=1000 -q ".[].sshUrl" | xargs -n1 git clone
-n1 option sets the expected number of parameters for xargs, which is one in this case. So, the above command is basically doing this:
git clone git@github.com:img9417/minimalist-blog.git
git clone git@github.com:img9417/portfolio_v2.git
git clone git@github.com:img9417/img9417.github.io.git