Skip to content

Instantly share code, notes, and snippets.

@maxrodrigo
Created September 15, 2021 06:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save maxrodrigo/8640621226261cbef1a13d0fc071a4e6 to your computer and use it in GitHub Desktop.
Prune Git Repository
#!/usr/bin/env bash
FILE="$1"
git filter-branch --index-filter "git rm --cached --ignore-unmatch $FILE" --prune-empty --tag-name-filter cat -- --all
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
#! /bin/sh
# get temporary file to hold viewed object hashes
TF=$(mktemp)
trap "rm -f $TF" 0 1 2 3 15
BIG=100000 # files up to (and including?) this size are not-big
git rev-list --all --topo-order --reverse |
while read commithash; do
git ls-tree -r $commithash |
while read mode type objhash path; do
[ $type == blob ] || continue # only look at files
blobsize=$(git cat-file -s $objhash)
[ $blobsize -le $BIG ] && continue # or -le
# found a big file - have we seen it yet?
grep $objhash $TF >/dev/null && continue
echo "$blobsize byte file added at commit $commithash as $path"
echo $objhash >> $TF # don't print again under any path name
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment