Skip to content

Instantly share code, notes, and snippets.

@ethomson
Created July 22, 2019 20:16
Show Gist options
  • Save ethomson/3e8e9cabf360917d52a689d57bcf1729 to your computer and use it in GitHub Desktop.
Save ethomson/3e8e9cabf360917d52a689d57bcf1729 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eufo pipefail
if [ "$#" -ne 2 ]; then
echo "usage: $0 source_repo_url target_repo_url" >&2
exit 1
fi
SOURCE_URL="$1"
TARGET_URL="$2"
WORKDIR="$(mktemp -d)"
echo "Cloning from ${SOURCE_URL} into ${WORKDIR}..."
git init --bare "${WORKDIR}"
cd "${WORKDIR}"
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}; pushing to ${TARGET_URL}"
git push --mirror "${TARGET_URL}"
echo ""
echo "Cleaning up temporary directory ${WORKDIR}..."
rm -rf "${WORKDIR}"
echo "Done."
@weitzman
Copy link

This helped me today. Thanks.

@maxenc7
Copy link

maxenc7 commented Mar 7, 2024

Instead of
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

why don't you use this?
git clone --mirror "${SOURCE_URL}"

Is there any special reason? @ethomson

@ethomson
Copy link
Author

ethomson commented Mar 7, 2024

@maxenc7 I wrote this 5 years ago. I don't remember precisely what my goals were but I suspect that it was intentionally done to limit the refs that were mirrored. If you git clone --mirror a repository on GitHub, you'll get all the pull request refs (refs/pull/<number>/head and refs/pull/<number>/merge). And all the objects that are reachable from those pull request refs. So specifying only a ref refspecs will give you the state of the repository (branches, tags, notes) without any of the things that were never actually merged into a branch.

Why I wanted to do this, I don't know, and maybe there's a way to make git clone --mirror do this as well, I don't know that either. 🤷

@ethomson
Copy link
Author

ethomson commented Mar 7, 2024

Indeed - I wrote about it here: https://www.edwardthomson.com/blog/advent_day_23_removing_large_binaries_with_lfs.html but didn't actually link to the script. And somebody else put some helpful comments in a fork of this gist so that I would know what I was thinking. https://gist.github.com/Fishezzz/12e94d9a06daaa9af461c897583fe3fd

(Thanks @Fishezzz)

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