Created
January 7, 2018 12:52
-
-
Save rubenmromero/9d36ff7c88872748d1de2f61c5d79602 to your computer and use it in GitHub Desktop.
Bash :: Find and remove large files in Git history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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