Skip to content

Instantly share code, notes, and snippets.

@Skinner927
Last active August 31, 2022 19:00
Show Gist options
  • Save Skinner927/2c5deef4353bdd4eba0e6274c40ef2bd to your computer and use it in GitHub Desktop.
Save Skinner927/2c5deef4353bdd4eba0e6274c40ef2bd to your computer and use it in GitHub Desktop.
Mirror entire git repo -- all branches, tags, and notes
#!/bin/sh
# Mirrors an entire repo with al tags and branches.
# Based off this article.
# https://www.edwardthomson.com/blog/mirroring_git_repositories.html
set -eufo pipefail
if [ "$#" -lt 1 ]; then
echo "usage: $0 SOURCE_REPO_URL [PACKAGED_NAME]" >&2
exit 1
fi
SOURCE_URL="$1"
if [ -z "${2:-}" ]; then
REPONAME="$(basename "$SOURCE_URL")-mirror"
else
REPONAME="$2"
fi
TMP_ROOT="$(mktemp -d)"
WORKDIR="${TMP_ROOT}/${REPONAME}"
mkdir -p "$WORKDIR"
WORKDIR="$(cd "$WORKDIR"; pwd -P)"
echo "Cloning from ${SOURCE_URL} into ${WORKDIR}..."
HERE="$PWD"
cd "${WORKDIR}"
git init --bare .
git config remote.origin.url "${SOURCE_URL}"
git config --add remote.origin.fetch '+refs/heads/*:refs/heads/*'
git config --add remote.origin.fetch '+refs/tags/*:refs/tags/*'
git config --add remote.origin.fetch '+refs/notes/*:refs/notes/*'
git config remote.origin.mirror true
git fetch --all
echo ''
echo "Cloned to ${WORKDIR}"
echo ''
ZIP_FILE="${REPONAME}.zip"
cd ..
zip -r "$ZIP_FILE" "$REPONAME"
mv "$ZIP_FILE" "${HERE}/"
rm -rf "$TMP_ROOT"
echo ''
echo "Mirrored repo zipped into ${PWD}/${ZIP_FILE}"
echo 'Upload with: git push --mirror TARGET_URL'
echo ''
echo 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment