Git clone multiple repositories

Installting or reinstalling too many machines all the time? Us too. This might help getting your repositories cloned to your machine a bit faster.

Gitlab

Requires read_api scope access under the Personal Access Tokens

If you do this on rare occasions, it might be best to set a short expiry date when generating your token and just create a new one each time. Faster than cloning manually but won’t leave the token active without being used.

Replace with your own token, as the one in the example does not work.

You can save the access token in a file or in an environmental variable like this:

export gitlab_read_api_access_token="dyHNrSc5zVD8cAKzJ4ds"
echo "${gitlab_read_api_access_token}"

Make sure you have your SSH key loaded, as this assumes SSH. You can read about generating and loading SSH key pairs on our blog.

Gitlab one liner to create git directory, cd to it and clone all repositories the user is a member of. Make sure there is enough disk space available. And it might take a few minutes or more as well…

mkdir -p "${HOME}"/git && cd "${HOME}"/git && curl --header "PRIVATE-TOKEN: ${gitlab_read_api_access_token}" "https://gitlab.com/api/v4/projects/?simple=yes&page=1&membership=true&private=true&per_page=100" | python3 -c $'import json, sys, os\nfor repo in json.load(sys.stdin): os.system("git clone " + repo["ssh_url_to_repo"])'

If this is the first time on your new machine, you will get a question to accept the SSH key.

This should be enough to clone down and get working on all your repositories.

Other options

If you want to check the command first you can either pipe it to jq:

curl --header "PRIVATE-TOKEN: ${gitlab_read_api_access_token}" "https://gitlab.com/api/v4/projects/?simple=yes&page=1&membership=true&private=true&per_page=100" | jq

Or change the os.system to a python print statement instead to print the full command that will be executed:

curl --header "PRIVATE-TOKEN: ${gitlab_read_api_access_token}" "https://gitlab.com/api/v4/projects/?simple=yes&page=1&membership=true&private=true&per_page=100" | python3 -c $'import json, sys, os\nfor repo in json.load(sys.stdin): print("git clone " + repo["ssh_url_to_repo"])'

Or count the number of projects so you can compare with our Projects page:

curl --header "PRIVATE-TOKEN: ${gitlab_read_api_access_token}" "https://gitlab.com/api/v4/projects/?simple=yes&page=1&membership=true&private=true&per_page=100" | wc -l

Get public repositories only, which does not require a token at all:

curl "https://gitlab.com/api/v4/users/bendoin/projects/?simple=yes" | python3 -c $'import json, sys, os\nfor repo in json.load(sys.stdin): print("git clone " + repo["ssh_url_to_repo"])'

Clone starred projects only:

curl --header "PRIVATE-TOKEN: ${gitlab_read_api_access_token}" "https://gitlab.com/api/v4/users/bendoin/starred_projects/?simple=yes&private=true&per_page=1000&page=1" | python3 -c $'import json, sys, os\nfor repo in json.load(sys.stdin): os.system("git clone " + repo["ssh_url_to_repo"])'

Github

To Be Determined

Sources


Make sure to always keep your Operating System(s) and all of the software that you are using up to date.

Feedback on our content or did you find a bug somewhere?

Send us an email to feedback at this domain.

kthxbai