Skip to content

Instantly share code, notes, and snippets.

@ntanis-dev
Last active April 28, 2025 21:33
Show Gist options
  • Save ntanis-dev/1a64c535757992515c1f9effc2d1bbf7 to your computer and use it in GitHub Desktop.
Save ntanis-dev/1a64c535757992515c1f9effc2d1bbf7 to your computer and use it in GitHub Desktop.
Checks and displays the size of Git staged files. Tested in Ubuntu 22.04 LTS.
#!/bin/bash
num_files=$(git --no-pager diff --cached --name-only | wc -l)
if [ "$num_files" -eq 0 ]; then
echo -e "\033[91mERROR: You have no staged files.\033[0m"
exit 0
fi
total_size=$(git --no-pager diff --cached --name-only -z | xargs -0 du -b | awk '{total += $1} END {print total / 1024 / 1024}')
total_size_int=$(echo $total_size | cut -d. -f1)
echo -e "\033[36m${num_files} staged file(s)\033[0m | \033[33m≈$(printf "%.2f" $total_size) MB total size\033[0m"
if [ $total_size_int -gt 1900 ]; then
echo -e "\033[91mWARNING: The total size of staged files is close to or greater than the GitHub push limit of 2GB.\033[0m"
fi
@ntanis-dev
Copy link
Author

ntanis-dev commented Feb 24, 2024

Terminal one-liner in your repo's directory:

curl -s https://gist.githubusercontent.com/ntanis-dev/1a64c535757992515c1f9effc2d1bbf7/raw/git_staged_files_size_check.sh | bash

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