Check Out My Results

My requirement: I want to read Linux source code, manage my Linux source code reading with git, and save my source code reading with my self-built GitLab. Then I discovered that simply using git push for a 2GB Linux source code file would cause problems.
So I wrote a script to solve this problem.
The script content is as follows:
shell#!/bin/bash# Configuration parameters (modify as needed)REMOTE_NAME="origin" # Remote repository nameBRANCH_NAME="main" # Target branch nameREMOTE_URL="git@gitlab.cheverjohn.me:CheverJohn/linux.git" # Remote repository URLBATCH_SIZE=500 # Number of files per batchCOMMIT_MESSAGE="Batch file upload" # Commit message# Color configurationRED='\033[0;31m'GREEN='\033[0;32m'YELLOW='\033[0;33m'BLUE='\033[0;34m'NC='\033[0m' # No color# Check if we're inside a git repositoryif ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; thenecho -e "${YELLOW}Current directory is not a git repository, initializing...${NC}"git initecho -e "${GREEN}Git repository initialized${NC}"elseecho -e "${GREEN}Git repository already exists${NC}"fi# Check if remote repository is configuredif ! git remote | grep -q "$REMOTE_NAME"; thenecho -e "${YELLOW}Adding remote repository $REMOTE_NAME: $REMOTE_URL${NC}"git remote add $REMOTE_NAME $REMOTE_URLecho -e "${GREEN}Remote repository added${NC}"elseCURRENT_URL=$(git remote get-url $REMOTE_NAME 2>/dev/null || echo "")if [ "$CURRENT_URL" != "$REMOTE_URL" ]; thenecho -e "${YELLOW}Updating remote repository URL: $REMOTE_URL${NC}"git remote set-url $REMOTE_NAME $REMOTE_URLecho -e "${GREEN}Remote repository URL updated${NC}"elseecho -e "${GREEN}Remote repository correctly configured${NC}"fifi# Ensure main branch existsif ! git show-ref --quiet refs/heads/$BRANCH_NAME; thenecho -e "${YELLOW}Creating branch $BRANCH_NAME...${NC}"git checkout -b $BRANCH_NAMEecho -e "${GREEN}Branch $BRANCH_NAME created${NC}"elseecho -e "${GREEN}Branch $BRANCH_NAME already exists${NC}"git checkout $BRANCH_NAMEfi# Get all untracked and modified filesecho -e "${BLUE}Getting list of files to upload...${NC}"FILES=($(git ls-files --others --exclude-standard) $(git diff --name-only))TOTAL_FILES=${#FILES[@]}if [ $TOTAL_FILES -eq 0 ]; thenecho -e "${YELLOW}No files found to add, trying to add all files...${NC}"git add -AFILES=($(git diff --name-only --cached))TOTAL_FILES=${#FILES[@]}if [ $TOTAL_FILES -eq 0 ]; thenecho -e "${RED}Error: No files found to push${NC}"exit 1fielse# Add all files to staging areaecho -e "${BLUE}Adding all files to staging area...${NC}"git add -Afiecho -e "${GREEN}Found $TOTAL_FILES files to upload${NC}"# Calculate number of batchesBATCH_COUNT=$(( ($TOTAL_FILES + $BATCH_SIZE - 1) / $BATCH_SIZE ))echo -e "${GREEN}Will upload in $BATCH_COUNT batches${NC}"# First commit all filesecho -e "${BLUE}Committing all files...${NC}"git commit -m "$COMMIT_MESSAGE"# Push in batchesecho -e "${BLUE}Starting batch push...${NC}"git push -u $REMOTE_NAME $BRANCH_NAMEif [ $? -eq 0 ]; thenecho -e "${GREEN}All files successfully pushed to remote repository${NC}"elseecho -e "${YELLOW}Regular push failed, trying batch push method...${NC}"# Using git batch push method (referenced from search results examples)# Based on git_batch_push.sh concept, but simplified implementation# Use git rev-list to get all commitsCOMMITS=($(git rev-list --reverse HEAD))TOTAL_COMMITS=${#COMMITS[@]}if [ $TOTAL_COMMITS -eq 0 ]; thenecho -e "${RED}Error: No commits found${NC}"exit 1fiecho -e "${GREEN}Found $TOTAL_COMMITS commits, will push in batches${NC}"# Calculate number of batches (500 commits per batch)COMMIT_BATCH_SIZE=500COMMIT_BATCH_COUNT=$(( ($TOTAL_COMMITS + $COMMIT_BATCH_SIZE - 1) / $COMMIT_BATCH_SIZE ))echo -e "${GREEN}Will push commits in $COMMIT_BATCH_COUNT batches${NC}"# Push commits in batchesfor ((i=0; i<$COMMIT_BATCH_COUNT; i++)); doSTART=$(($i * $COMMIT_BATCH_SIZE))END=$((($i + 1) * $COMMIT_BATCH_SIZE))if [ $END -gt $TOTAL_COMMITS ]; thenEND=$TOTAL_COMMITSfiBATCH_END_INDEX=$((END - 1))TARGET_COMMIT=${COMMITS[$BATCH_END_INDEX]}echo -e "${BLUE}Pushing batch $((i+1))/${COMMIT_BATCH_COUNT} (commits $((START+1))-$END)...${NC}"if git push $REMOTE_NAME $TARGET_COMMIT:refs/heads/$BRANCH_NAME; thenecho -e "${GREEN}Batch $((i+1)) successfully pushed${NC}"elseecho -e "${RED}Batch $((i+1)) push failed${NC}"echo -e "${YELLOW}Trying alternative push method...${NC}"# If above method fails, try another batch push methodif [ $i -eq 0 ]; then# First batch, create new branchgit push $REMOTE_NAME $BRANCH_NAMEelse# Get the end commit of previous batchPREV_END=$(($START - 1))PREV_COMMIT=${COMMITS[$PREV_END]}CURR_COMMIT=${COMMITS[$BATCH_END_INDEX]}echo -e "${BLUE}Using range push $PREV_COMMIT..$CURR_COMMIT${NC}"git push $REMOTE_NAME $PREV_COMMIT:refs/heads/$BRANCH_NAME $CURR_COMMIT:refs/heads/$BRANCH_NAMEfifiechodonefiecho -e "${GREEN}Batch upload process completed!${NC}"
The usage method is simple.
shellchmod +x git_batch_push.sh
Then run the script in the current folder.
Final Results

Comments