Skip to content

Instantly share code, notes, and snippets.

@alwinw
Last active April 26, 2020 02:05
Show Gist options
  • Select an option

  • Save alwinw/73c3e46348cb75fb18df64d24cf7b2df to your computer and use it in GitHub Desktop.

Select an option

Save alwinw/73c3e46348cb75fb18df64d24cf7b2df to your computer and use it in GitHub Desktop.
Git pull all branches from another remote and push them as their own group
#!/bin/sh
MIRROR_REPO=${MIRROR_REPO:-"origin"}
UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-"upstream_remote"}
UPSTREAM_REMOTE_NAME=${UPSTREAM_REMOTE_NAME:-"upstream"}
UPSTREAM_REMOTE_URL=${UPSTREAM_REMOTE_URL:-"<alternate-remote.git>"}
echo "Pulling $UPSTREAM_REMOTE_URL"
git remote add "$UPSTREAM_REMOTE" "$UPSTREAM_REMOTE_URL"
git fetch "$UPSTREAM_REMOTE"
for branch in $(git ls-remote $UPSTREAM_REMOTE | grep -oP '(?<=refs/heads/).*'); do
echo "Fetching branch '$branch' and setting upstream to '$MIRROR_REPO/$UPSTREAM_REMOTE_NAME/$branch'"
git fetch "$UPSTREAM_REMOTE" "$branch:$UPSTREAM_REMOTE_NAME/$branch"
git push --set-upstream "$MIRROR_REPO" "$UPSTREAM_REMOTE_NAME/$branch:$UPSTREAM_REMOTE_NAME/$branch"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment