Here is a Shell script that includes multiple methods for incremental cloning., This script covers shallow cloning, cloning specific branches, cloning by tag, and sparse checkout.
#!/bin/bash
# Set repository URL and directory name
REPO_URL="https://github.com/user/repository.git"
REPO_DIR="repository"
# Shallow clone, defaults to cloning only the latest commit
shallow_clone() {
echo "Performing a shallow clone with depth 1..."
git clone --depth 1 $REPO_URL $REPO_DIR
}
# Clone a specific branch
clone_specific_branch() {
BRANCH_NAME="main"
echo "Cloning branch $BRANCH_NAME..."
git clone --branch $BRANCH_NAME --single-branch $REPO_URL $REPO_DIR
}
# Clone by tag
clone_by_tag() {
TAG_NAME="v1.0.0"
echo "Cloning tag $TAG_NAME..."
git clone $REPO_URL $REPO_DIR --no-checkout
cd $REPO_DIR
git checkout tags/$TAG_NAME
}
# Incremental clone to gradually fetch history
incremental_clone() {
DEPTH=10
echo "Performing a shallow clone with depth $DEPTH..."
git clone --depth $DEPTH $REPO_URL $REPO_DIR
cd $REPO_DIR
while true; do
echo "Fetching additional history..."
DEPTH=$((DEPTH + 10))
git fetch --depth=$DEPTH
echo "Continue fetching more history? (y/n)"
read -r CONTINUE
if [ "$CONTINUE" != "y" ]; then
break
fi
done
echo "Fetching full history..."
git fetch --unshallow
}
# Sparse checkout (cloning specific directories)
sparse_checkout() {
DIRECTORY="src"
echo "Performing sparse checkout for directory $DIRECTORY..."
git clone --no-checkout $REPO_URL $REPO_DIR
cd $REPO_DIR
git sparse-checkout init --cone
git sparse-checkout set $DIRECTORY
git checkout
}
# Main menu
echo "Select an option:"
echo "1) Shallow clone"
echo "2) Clone specific branch"
echo "3) Clone by tag"
echo "4) Incremental clone"
echo "5) Sparse checkout"
echo "6) Exit"
read -r OPTION
case $OPTION in
1)
shallow_clone
;;
2)
clone_specific_branch
;;
3)
clone_by_tag
;;
4)
incremental_clone
;;
5)
sparse_checkout
;;
6)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid option"
;;
esac
How to Use This Script
- Save the script content to a file, for example,
git_clone_steps.sh
. - Make the script executable:
chmod +x git_clone_steps.sh
- Run the script:
./git_clone_steps.sh
The script will prompt you to select which cloning method to use. Follow the prompts to execute your chosen method. You can adjust the repository URL, branch name, tag name, and directory path according to your needs.