Skip to content

Instantly share code, notes, and snippets.

@AndrianBdn
Last active May 4, 2022 23:06
Show Gist options
  • Save AndrianBdn/ae8cedaec5a5df30fdbf to your computer and use it in GitHub Desktop.
Save AndrianBdn/ae8cedaec5a5df30fdbf 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