Created
December 15, 2024 06:41
-
-
Save Lunarixus/9c363e19e29a0dc27b8d08f405d12fcb to your computer and use it in GitHub Desktop.
Push to GitHub in chunks
This file contains hidden or 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/bash | |
# Ensure the script exits on any command failure | |
set -e | |
# Variables | |
REMOTE="git-private" | |
BRANCH="android-15-staging" | |
COMMIT_LIMIT=50000 | |
# Verify that the current branch is pointing to HEAD | |
if ! git rev-parse --verify HEAD &>/dev/null; then | |
echo "Error: Current directory is not a Git repository or HEAD is invalid." | |
exit 1 | |
fi | |
# Count the total number of commits in the repository | |
TOTAL_COMMITS=$(git rev-list --count HEAD) | |
# Calculate the number of chunks required | |
CHUNK_COUNT=$(( (TOTAL_COMMITS + COMMIT_LIMIT - 1) / COMMIT_LIMIT )) | |
echo "Total commits: $TOTAL_COMMITS" | |
echo "Commits per push: $COMMIT_LIMIT" | |
echo "Number of chunks: $CHUNK_COUNT" | |
# Push chunks of commits | |
for (( i=0; i<CHUNK_COUNT; i++ )); do | |
START=$(( i * COMMIT_LIMIT + 1 )) | |
END=$(( START + COMMIT_LIMIT - 1 )) | |
if [ "$END" -gt "$TOTAL_COMMITS" ]; then | |
END=$TOTAL_COMMITS | |
fi | |
# Create a temporary branch for the current chunk | |
TMP_BRANCH="temp-chunk-$i" | |
# Check if the temporary branch already exists | |
if git show-ref --verify --quiet "refs/heads/$TMP_BRANCH"; then | |
echo "Branch $TMP_BRANCH already exists." | |
read -p "Do you want to delete it? (y/n): " RESPONSE | |
if [[ "$RESPONSE" != "y" ]]; then | |
echo "Exiting script as branch $TMP_BRANCH already exists." | |
exit 1 | |
fi | |
echo "Deleting existing branch $TMP_BRANCH..." | |
git branch -D "$TMP_BRANCH" | |
fi | |
echo "Creating temporary branch $TMP_BRANCH for commits $START to $END..." | |
COMMIT_HASH=$(git rev-list HEAD --reverse | sed -n "${END}p") | |
git branch "$TMP_BRANCH" "$COMMIT_HASH" | |
# Push the chunk to the remote branch | |
echo "Pushing $TMP_BRANCH to $REMOTE:$BRANCH..." | |
git push "$REMOTE" "$TMP_BRANCH:$BRANCH" --force | |
# Delete the temporary branch | |
echo "Deleting temporary branch $TMP_BRANCH..." | |
git branch -D "$TMP_BRANCH" | |
done | |
echo "All chunks pushed successfully to $REMOTE:$BRANCH." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment