Skip to content

Instantly share code, notes, and snippets.

@iso2022jp
Last active September 16, 2024 02:33
Show Gist options
  • Save iso2022jp/2acab64bca9b2a8b6d53e6eb9b2a7204 to your computer and use it in GitHub Desktop.
Save iso2022jp/2acab64bca9b2a8b6d53e6eb9b2a7204 to your computer and use it in GitHub Desktop.
Copy a single file from the remote git registory.
#!/bin/bash
# Copy the single file from the remote git registory.
set -euo pipefail
if [[ $# -lt 3 ]]; then
echo "Usage: $0 <repos> <branch> <src> [<dest>]">&2
exit 1;
fi
repos="$1"
branch="$2"
src="${3#/}"
dest="${4:-.}"
work="$(mktemp -d)"
git clone --depth=1 --branch="$branch" --filter=blob:none --no-checkout "$repos" "$work"
trap "rm -rf ${work@Q}" EXIT
(
cd "$work"
git sparse-checkout set --no-cone "/$src"
git checkout
)
cp "$work/$src" "$dest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment