git: reducing repository size (gc and destructive)
This article discusses two approaches to reducing the size of a Git repository: non-destructive garbage collection and destructive history rewriting. It provides a script for running git gc on multiple repositories and links to resources for more advanced techniques.
Garbage Collection (non-destructive)
This works especially well when removing a file added in the most recent unpushed commit. Git Garbage Collection automates some of these cleanup jobs.
I ran the following over my source folders:
for gitPath in $(find . -type d -name ".git" -readable -prune -exec realpath {} \; 2>/dev/null); do
cd $gitPath
echo ${gitPath}
# git branch
sizeBefore=$(du -sh . | cut -f1)
git fetch -p
git branch --format "%(refname:short)" | grep -vE "^(develop|master|staging)$" | xargs git branch -D
git gc --aggressive --prune=now
sizeAfter=$(du -sh . | cut -f1)
echo "${sizeBefore} -> ${sizeAfter}"
done
Reduce repository size (destructive)
Atlassian has a pretty good article about reducing git repository size. Also take a look at Git Help: removing-sensitive-data-from-a-repository and the GIT BFG.