Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash :: Find and remove large files in Git history
#!/bin/bash
set -o verbose
# Checkout and pull in the local copy of all existing branches in the repository
for branch in $(git branch -a |grep remotes/origin |grep -v HEAD |sed "s/remotes\/origin\///g")
do
git checkout $branch
git pull
done
# Get the existing large files in Git history
git rev-list master | while read rev; do git ls-tree -lr $rev | cut -c54- | grep -v '^ '; done | sort -u | perl -e '
while (<>) {
chomp;
@stuff=split("\t");
$sums{$stuff[1]} += $stuff[0];
}
print "$sums{$_} $_\n" for (keys %sums);
' | sort -rn >> large_files.txt
# Remove the large files stored in the 'large_files.txt' file
for file in $(cat large_files.txt |awk '{print $2}')
do
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch $file" --prune-empty --tag-name-filter cat -- --all
done
# Force-push the local changes made in the branches of the repository
git push origin --force --all
# Force-push the local changes made in the tags of the repository
git push origin --force --tags
# Remove the 'large_files.txt' file
rm large_files.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment