Skip to content

Instantly share code, notes, and snippets.

@neggles
Created August 17, 2022 15:00
Show Gist options
  • Save neggles/a2355b09ba6aec2303cc7a9618a9a036 to your computer and use it in GitHub Desktop.
Save neggles/a2355b09ba6aec2303cc7a9618a9a036 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu
# number of commits to push at once
batch_size=${1:-'100'}
# tgt_remote name to push to
tgt_remote=${2:-'origin'}
# git options
gitopts=${3:-''}
# some definitions for colors
LRED='\033[1;31m'
LGRN='\033[1;32m'
LPUR='\033[1;35m'
LCYA='\033[1;36m'
LWHT='\033[1;37m'
RST='\033[0m'
# get current branch name
push_branch=$(git rev-parse --abbrev-ref HEAD)
# check if the branch exists on the tgt_remote
if git show-ref --quiet --verify "refs/remotes/${tgt_remote}/${push_branch}"; then
# only push commits not already pushed to the tgt_remote
range="${tgt_remote}/${push_branch}..HEAD"
else
# push all the commits
range='HEAD'
fi
echo -e "${LWHT}Pushing ${LCYA}${range}${LWHT} to remote ${LGRN}${tgt_remote}${RST}"
# count the number of commits to push
commit_count=$(git log --first-parent --format=format:x ${range} | wc -l)
batch_count=$(( (commit_count / batch_size) + 1 ))
echo -e "${LPUR}${commit_count}${LWHT} refs to push, ${LPUR}${batch_count}${LWHT} batches of ${LPUR}${batch_size}${LWHT}${RST}\n"
# push each batch
for i in $(seq "$commit_count" "-$batch_size" 1); do
# get the hash of the commit to push
hash=$(git log --first-parent --reverse --format=format:%H --skip "$i" -n1)
batch=$(( (commit_count - i) / batch_size ))
echo -e "${LWHT} push batch ${LGRN}${batch}${LWHT}/${LCYA}${batch_count}:${RST}\n"
git push "${tgt_remote}" "${hash}:refs/heads/${push_branch}" ${gitopts}
echo ''
done
# push the final partial batch
echo -e "${LWHT} push final batch ${LCYA}${batch_count}:${RST}\n"
git push "${tgt_remote}" "${push_branch}" ${gitopts}
echo -e "${LGRN}Done!${RST}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment