#! /bin/bash set -e -o pipefail prog_name=$(basename ${0}) if [[ -z "$1" || -z "$2" ]] then echo "Usage: ${prog_name} []" exit 1 fi REPO=$1 SUBDIR=$2 [[ -n $3 ]] && DEST=${3} || DEST=$(basename ${REPO} .git) echo "[${prog_name}] Repository: ${REPO}" echo "[${prog_name}] Subdirectory: ${SUBDIR}" echo "[${prog_name}] Destination: ${DEST}" # Clone without populating working tree git clone --filter=blob:none --no-checkout --depth 1 ${REPO} ${DEST} # Enter repo cd ${DEST} # *SET* (not `add`) subdirectory to sparse-checkout file git sparse-checkout set ${SUBDIR} # Checkout dir git checkout