Skip to content

Instantly share code, notes, and snippets.

@eroullit
Created July 4, 2023 14:18
Show Gist options
  • Save eroullit/b7576fba48f89109f58fe0fe104deec9 to your computer and use it in GitHub Desktop.
Save eroullit/b7576fba48f89109f58fe0fe104deec9 to your computer and use it in GitHub Desktop.
Git Push chunk by chunk
#!/bin/bash
set -eu
# Name of the remote (usually origin)
REMOTE=origin
# Find the name of the current branch
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Push commit in batches of size.
# The larger the batch size, the larger the amount of data will
# be pushed in one go and the faster it will get uploaded
BATCH_SIZE=10
# 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
echo "Range: $range"
# count the number of commits to push
n=$(git log --first-parent --format=format:x $range | wc -l)
echo "Commits to push: $n"
# 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:$BRANCH --force
done
# push the final partial batch
git push $REMOTE HEAD:$BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment