Last active
April 17, 2023 16:59
-
-
Save hiranp/a26e334369386211709f4846929a6157 to your computer and use it in GitHub Desktop.
Shell script to extract specific folder from remote git repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env bash | |
# This script clones the remote repository using the --filter=blob:none option to avoid downloading any file contents. | |
# It then checks out the specified remote branch and enables sparse-checkout. The sparse-checkout pattern is set to only | |
# include the desired folder, and finally, the latest changes are pulled from the remote branch. | |
# NOTE: | |
# Customize this script by setting the REMOTE_REPO_URL, REMOTE_BRANCH, GIT_FOLDER_PATH, and LOCAL_REPO_PATH variables. | |
# Set the remote repository URL | |
REMOTE_REPO_URL="https://github.com/repository.git" | |
# Set the remote branch name | |
REMOTE_BRANCH="master" | |
# Set the path to the folder you want to copy | |
GIT_FOLDER_PATH="example" | |
# Set the path to the local repository | |
LOCAL_REPO_PATH="${PWD}/${GIT_FOLDER_PATH}" | |
echo "Cloning the remote repository...to ${LOCAL_REPO_PATH}" | |
if [ ! -d "${LOCAL_REPO_PATH}" ]; then | |
mkdir -p "${LOCAL_REPO_PATH}" | |
# Shadow clone the remote repository | |
git clone --depth 1 --no-checkout --filter=blob:none "${REMOTE_REPO_URL}" "${LOCAL_REPO_PATH}" | |
fi | |
# Change to the repository directory | |
cd "${LOCAL_REPO_PATH}" | |
# Checkout the remote branch | |
git checkout "${REMOTE_BRANCH}" | |
if [ ! -f ".git/info/sparse-checkout" ]; then | |
# Enable sparse-checkout | |
git sparse-checkout init | |
# Set the sparse-checkout pattern to only include the desired folder | |
git sparse-checkout set "${GIT_FOLDER_PATH}" | |
fi | |
# Pull the latest changes from the remote branch | |
git pull origin "${REMOTE_BRANCH}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment