Git
Push Big Repo

How to push a big repo?

Generally, git hosting providers have a limit of 2 GiB of pack size.

remote: fatal: pack exceeds maximum allowed size (2.00 GiB)

If history is big, this limit can hit easily. However, we can push in batches as follows:

#!/usr/bin/bash
 
# Configuration variables
REMOTE=origin                                # Remote repository name (e.g. origin, upstream)
BRANCH=$(git rev-parse --abbrev-ref HEAD)    # Get current branch name
BATCH_SIZE=500                               # Number of commits to push in each batch
 
# Check if the target branch exists on the remote repository
if git show-ref --quiet --verify refs/remotes/$REMOTE/$BRANCH; then
    # Branch exists: only push new commits that aren't on the remote
    range=$REMOTE/$BRANCH..HEAD
else
    # Branch doesn't exist: push all commits
    range=HEAD
fi
 
# Count total number of commits to be pushed using first-parent to follow main line of history
n=$(git log --first-parent --format=format:x $range | wc -l)
 
# Push commits in batches to stay under repository size limits
for i in $(seq $n -$BATCH_SIZE 1); do
    # Get the hash of the oldest commit in current batch
    h=$(git log --first-parent --reverse --format=format:%H --skip $i -n1)
    echo "Pushing $h..."
    git push $REMOTE ${h}:refs/heads/$BRANCH
done
 
# Push any remaining commits (less than BATCH_SIZE) in the final batch
git push $REMOTE HEAD:refs/heads/$BRANCH