Skip to content

Instantly share code, notes, and snippets.

@nutrino
Forked from AndrianBdn/git-sparse-clone.sh
Created May 4, 2022 23:06
Show Gist options
  • Save nutrino/ad741ea1281d3ee7e0e0ebdf9f3afb3a to your computer and use it in GitHub Desktop.
Save nutrino/ad741ea1281d3ee7e0e0ebdf9f3afb3a to your computer and use it in GitHub Desktop.
git-sparse-clone.sh
#!/bin/sh
#
# This script can clone/checkout a single folder from git repository
# - Might be used for checking out micro-services from monolithic git repository
#
# - You can even do checkout into home directory, for example
# git-sparse-clone.sh [email protected]:readdle/fluix-web.git /home/login login
#
# This will cause the shell to exit immediately if a simple command exits with a nonzero exit value.
set -e
[ $# -ne 3 ] && { echo "Usage: $0 <repository-URL> <repository-path> <local-repo-root-directory>"; exit 1; }
repo_url=$1
repo_path=$2
local_repo_root=$3
git clone $repo_url $local_repo_root
cd $local_repo_root
git config core.sparsecheckout true
echo "/$repo_path/**" > .git/info/sparse-checkout
git read-tree -m -u HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment