Skip to content

Instantly share code, notes, and snippets.

@Sacristan
Last active June 3, 2024 12:54
Show Gist options
  • Save Sacristan/8e759e6d7627a9ba4b26643869188426 to your computer and use it in GitHub Desktop.
Save Sacristan/8e759e6d7627a9ba4b26643869188426 to your computer and use it in GitHub Desktop.
Push commits by chunks
REMOTE=origin
BRANCH=$(git rev-parse --abbrev-ref HEAD)
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
range=HEAD
echo "Range: $r"
# 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/$BRANCH --force
done
# push the final partial batch
git push $REMOTE HEAD:refs/heads/$BRANCH
@miclgael
Copy link

miclgael commented Apr 6, 2021

🤩 This is so good - thanks, @Sacristan

Take a star ⭐

@2jiwon
Copy link

2jiwon commented Apr 13, 2021

man, you saved my day! Thank you for sharing this.

@Swooplezzz
Copy link

love it thx <3

@Sacristan
Copy link
Author

Thanks! :)

@yugr
Copy link

yugr commented Jan 28, 2022

I believe it should be

echo "Range: $range"

and set -eu won't hurt.

@jawadabbass
Copy link

Error:

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for.................................................

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment