Created
September 16, 2024 20:28
-
-
Save stephenlacy/8222944b19ceb435535206b2c584f5e1 to your computer and use it in GitHub Desktop.
Sync a massive git repo from local to remote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TARGET_BRANCH='upstream/release' | |
BATCH_SIZE=10000 | |
range=HEAD | |
git fetch $REMOTE $TARGET_BRANCH | |
# check if the branch exists on the remote | |
if git show-ref --quiet --verify refs/remotes/$REMOTE/$TARGET_BRANCH; then | |
# if so, only push the commits that are not on the remote already | |
range=$REMOTE/$TARGET_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:refs/heads/$TARGET_BRANCH # --force | |
done | |
# push the final partial batch | |
git push $REMOTE HEAD:refs/heads/$TARGET_BRANCH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment