• ᴥ •つ TOP

We can utilize GitHub command-line tool, gh, to clone all repositories.

Install gh using Homebrew or check their installation guide for other methods.

$ brew install gh $ gh --version # check if it installed successfully

Before we utilize the command-line tool, we need to authenticate gh and git with GitHub.

$ gh auth login

This command will bring up the GitHub page for you to grant permissions. Once done, you can return to the terminal. Give this command a try and see if it lists all of your repositories:

$ gh repo list --limit 1000

The gh repo list command displays a nice list of all repositories, but we're only interested in the URLs.

We'll use the --json=<field> option, which outputs JSON with the specified fields. The field we want is sshUrl.

$ gh repo list --json=sshUrl

The output looks like the following:

[ { "sshUrl": "git@github.com:rolemadelen/foo.git" }, { "sshUrl": "git@github.com:rolemadelen/bar.git" }, { "sshUrl": "git@github.com:rolemadelen/baz.git" }, { "sshUrl": "git@github.com:rolemadelen/qux.git" } ]

While we have the URLs in JSON format, we want to filter the JSON output using a jq expression so that we only have the URL as a string, not in JSON format.

$ gh repo list --json=sshUrl -q ".[].sshUrl"

Here's the output.

git@github.com:rolemadelen/foo.git git@github.com:rolemadelen/bar.git git@github.com:rolemadelen/baz.git git@github.com:rolemadelen/qux.git

Now, all we need to do is parse this string into the git clone command using the pipe.

$ gh repo list --json=sshUrl -q ".[].sshUrl" | xargs -n1 git clone

The -n1 option specifies that xargs should use at most one argument per command. Therefore, the above command will use each URL resulting from the first command before the pipe as an argument for the second command, which is git clone.

← prev postnext post →