Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active November 18, 2021 22:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanviscuso/a77abd8a2fc64de7f15de6e43dbd904b to your computer and use it in GitHub Desktop.
Save germanviscuso/a77abd8a2fc64de7f15de6e43dbd904b to your computer and use it in GitHub Desktop.
Mirror/Sync Github Organization Repos locally
#!/bin/bash
# Clone all org repos (via ssh)
curl -s https://api.github.com/orgs/<organization>/repos?per_page=200 | python -c $'import json, sys, os\nfor repo in json.load(sys.stdin): os.system("git clone " + repo["ssh_url"])'
# Later pull from each repo
back=`pwd`; for d in `find . -type d -name .git` ; do cd "$d/.."; git pull origin; cd $back ; done
@teeks99
Copy link

teeks99 commented Apr 27, 2018

Modifying it to os.system("git clone --bare "will mirror the bare repos, which can be nice if you want to be able to clone from the destination.

@csandman
Copy link

csandman commented Nov 17, 2021

The python part wasn't working for me in my server environment, or using the ssh_url instead of the clone_url. I also needed a version of this script that would work with private repos from an organization my account has access to. Here is my modified version of this:

#!/bin/bash

# Clone all org repos (via https)
curl --silent -i -u "<github_username>:<personal_access_token>" "https://api.github.com/orgs/<organization>/repos?per_page=200" | grep -oP '(?<="clone_url": ").+?(?=")' | while read line; do
  echo $line
  # Replace `github.com` in the clone url with `token@github.com`
  git clone ${line/github/"<personal_access_token>@github"}
done

# Later pull from each repo
back=$(pwd)
for d in $(find . -type d -name .git); do
  cd "$d/.."
  git pull origin
  cd $back
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment