Skip to content

Instantly share code, notes, and snippets.

@maxrodrigo
Last active August 28, 2024 08:29
Show Gist options
  • Save maxrodrigo/b893bf76f68588766d602a57f10c4ff8 to your computer and use it in GitHub Desktop.
Save maxrodrigo/b893bf76f68588766d602a57f10c4ff8 to your computer and use it in GitHub Desktop.

Revisions

  1. maxrodrigo renamed this gist Nov 19, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. maxrodrigo created this gist Nov 19, 2018.
    27 changes: 27 additions & 0 deletions split-git-push.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # Split a repository into batches to avoid `pack exceeds maximum allowed size` on git push

    REMOTE=origin
    BRANCH=$(git rev-parse --abbrev-ref HEAD)
    BATCH_SIZE=500

    # check if the branch exists on the remote
    if git show-ref --quiet --verify refs/remotes/$REMOTE/$BRANCH; then
    # if so, only push the commits that are not on the remote already
    range=$REMOTE/$BRANCH..HEAD
    else
    # else push all the commits
    range=HEAD
    fi
    # count the number of commits to push
    n=$(git log --first-parent --format=format:x $range | wc -l)

    # push each batch
    for i in $(seq $n -$BATCH_SIZE 1); do
    # get the hash of the commit to push
    h=$(git log --first-parent --reverse --format=format:%H --skip $i -n1)
    echo "Pushing $h..."
    git push $REMOTE $h:refs/heads/$BRANCH
    done

    # push the final partial batch
    git push $REMOTE HEAD:refs/heads/$BRANCH