Skip to content

Instantly share code, notes, and snippets.

@YahuiWong
Last active August 7, 2025 02:33
Show Gist options
  • Select an option

  • Save YahuiWong/e1f647b31232a5e1fcaa7bd16eeb22e3 to your computer and use it in GitHub Desktop.

Select an option

Save YahuiWong/e1f647b31232a5e1fcaa7bd16eeb22e3 to your computer and use it in GitHub Desktop.
git
#!/bin/bash
# Configuration
SIZE_THRESHOLD=1048576 # 1MB
# Check dependencies
if ! command -v git-filter-repo >/dev/null 2>&1; then
echo "Error: git-filter-repo is not installed. Please install it first: pip install git-filter-repo"
exit 1
fi
if ! command -v gawk >/dev/null 2>&1; then
echo "Error: gawk command not found. Please ensure Git Bash is properly installed."
exit 1
fi
if ! command -v od >/dev/null 2>&1; then
echo "Error: od command not found. Please ensure Git Bash is properly installed."
exit 1
fi
# Backup .git directory
echo "Backing up .git to .git-backup. To restore, rename .git-backup to .git"
cp -r .git .git-backup || { echo "Backup failed, exiting."; exit 1; }
# Check if SIZE_THRESHOLD is set, otherwise set a default (e.g., 1MB = 1048576 bytes)
SIZE_THRESHOLD=${SIZE_THRESHOLD:-1048576}
# Get list of large files from git history
files=$(git rev-list --objects --all | \
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
gawk -v threshold="$SIZE_THRESHOLD" '$1 == "blob" && $3 > threshold' | \
sort -nr -k3 | \
awk '{print $4}')
# Check if files were found
if [ -z "$files" ]; then
echo "No files larger than $SIZE_THRESHOLD bytes found."
exit 0
fi
# Loop through each file and remove it using git filter-repo
for file in $files; do
echo "If you are sure you want to delete the file: $file, please input Y or y to confirm, or any other key to skip"
read answer
if [ "$answer" == "Y" ] || [ "$answer" == "y" ]; then
echo "Removing file: $file"
git filter-repo --path "$file" --invert-paths --force
else
echo "Skipping file $file"
fi
done
echo "Large files have been removed. Please review the changes and force push if needed."
echo "To force push, use: git push origin --force --all"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment