Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Forked from kobake/git-commit-size.sh
Last active June 10, 2025 08:55
Show Gist options
  • Save turboBasic/7b5ac4b1524f19b4520ff61a4a9e78ed to your computer and use it in GitHub Desktop.
Save turboBasic/7b5ac4b1524f19b4520ff61a4a9e78ed to your computer and use it in GitHub Desktop.
Calculate size of a git commit object #git
# Calculate the size of a git commit in bytes.
function git-commit-size() {
# This script calculates the size of a git commit in bytes.
# Usage: git-commit-size.sh <commit hash>
#
# Example:
# git-commit-size.sh 1234567
#
# Output:
# The total size of the commit in bytes, formatted with thousands separators.
local BLOB_HASH_LIST COMMIT_SIZE HASH ITEM_LIST SIZE_LIST
if (( $# != 1 )); then
echo "Usage: git-commit-size.sh <commit hash>" 1>&2
return 1
fi
HASH=$1
# -r: recurse; -c: show differences from each parent of a merge commit simulaneously
ITEM_LIST=$(
git -c diff.renameLimit=0 diff-tree -r -c --find-copies --find-renames --no-commit-id "$HASH"
)
BLOB_HASH_LIST=$(echo "$ITEM_LIST" | awk '{ print $4 }')
SIZE_LIST="$(
echo "$BLOB_HASH_LIST" \
| git cat-file --batch-check \
| grep "blob" \
| awk '{ print $3}'
)"
COMMIT_SIZE="$(echo "$SIZE_LIST" | awk '{ sum += $1 } END { print sum }')"
LC_NUMERIC=en_US.UTF-8 printf "%'.0f\n" "$COMMIT_SIZE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment