7/17/2017 - 1:18 PM



DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

find $DIR -maxdepth 6 -type d -name ".git" -printf "%h\n" | while read line; do 
    echo "Directory: $line"
    size=$(du -s $line | cut -f1)
    git -C $line reflog expire --all --expire=now > /dev/null 2>>gc_errors.log
    git -C $line gc --prune=now --aggressive > /dev/null 2>>gc_errors.log
    newsize=$(du -s $line | cut -f1)
    if [[ $size == $newsize ]]; then
        echo "Same size, nothing was optimized"
        echo "From $size to $newsize, saved "$(($size - $newsize))" bytes"
    echo ""

Git gc script to save space

Place this at the "root" of your git repos, or even your projects directory.

Like /var/www/git_gc.bash for example, if you save many projects under this dir.

This script will run git reflog expire and git gc with extra options to save some space when working with heavy & often rebased projects.

Sometimes, it can save up to hundreds of megabytes or even gigabytes of space, depending on the project!

Be careful though, it can lead to huge CPU usage, make sure you run this when you are not working.

Enjoy 