Skip to content

Instantly share code, notes, and snippets.

@mausch
Created February 17, 2012 03:19
Show Gist options
  • Save mausch/1850280 to your computer and use it in GitHub Desktop.
Save mausch/1850280 to your computer and use it in GitHub Desktop.
Adds all github forks in the network for a particular repository
mainrepo="mausch/solrnet"
urls=$(curl -s "https://api.github.com/repos/$mainrepo/forks" | grep git_url | sed -re 's/.*: "(.*)",/\1/')
for url in $urls; do
remote=$(echo $url | cut -d/ -f4)
git remote add $remote $url
done
@ackalker
Copy link

Here's one that works for any Github repo. No need to hardcode the repo name, it gets it from .git/config. It also fetches more pages when there are more than 30 forks.

#!/bin/sh

# github-add-forks - Add all forks of a Github repository as remotes

! [ -d .git ] && echo "Error: Not in a Git repository." >&2 && exit 1

owner_repo=$(sed -nr -e '/\[remote "origin"]/{n;s|^\s*url = .*github.com/(.*)\.git$|\1|p}' .git/config)
[ -z "$owner_repo" ] && echo "Error: Repository not cloned from Github." >&2 && exit 1

lastpage_url=$(curl -s -I "https://api.github.com/repos/$owner_repo/forks" | \
        sed -nr -e 's/^Link: .*<([^>]*)>; rel="last".*$/\1/p')
lastpage=${lastpage_url#*?page=}
page_url_base=${lastpage_url%?page=*}

for page in $(seq 1 $lastpage); do
        page_url="$page_url_base?page=$page"
        repo_urls=$(curl -s "$page_url" | \
                sed -nr -e 's/^\s*"git_url": "(.*)",$/\1/p')
        for url in $repo_urls; do
                remote=$(echo $url | sed -nr -e 's|^.*github.com/(.*)/.*\.git$|\1|p')
                echo "Adding remote: $remote $url"
                git remote add $remote $url
        done
done

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