Script for cloning reference Git repos
I’ve written a POSIX shell script for cloning third-party Git repos to my workstation. It works like this:
$ clone github.com/pallets/click
downloaded to: ~/src/github.com/pallets/click
The repo name can optionally have a prefix of “http://” or “https://”, to make it easy to copy and paste a link into the terminal.
It does a partial clone (Git manual; GitHub blog post), so that only the contents of the default branch get downloaded, saving a bit of disk space and quite a bit of time when fetching large repos. It still retrieves the whole history, and doesn’t require extra admin when checking out older commits or other branches, unlike with shallow clones.
#!/bin/sh -e
if [ -z "$1" ]; then
echo "usage: $0 repo_url" >&2
exit 1
fi
# strip protocol prefix if present
REPO=${1#https://}
REPO=${REPO#http://}
URL=https://$REPO
OUT_DIR=src/$REPO
cd
git clone --filter=blob:none "$URL" "$OUT_DIR"
echo "downloaded to: ~/$OUT_DIR" >&2