Skip to content

Instantly share code, notes, and snippets.

@Pierstoval
Last active April 2, 2019 14:30
Show Gist options
  • Select an option

  • Save Pierstoval/ed3c24ac4d143fd42dc4c0717445eb7e to your computer and use it in GitHub Desktop.

Select an option

Save Pierstoval/ed3c24ac4d143fd42dc4c0717445eb7e to your computer and use it in GitHub Desktop.

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 😃

#!/bin/bash
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"
else
echo "From $size to $newsize, saved "$(($size - $newsize))" bytes"
fi
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment