Run "git gc" on all repos in a directory

Jacob Allred
#linux

I occasionally like to run git gc on all my git repositories. I store the master copy of each repo in a directory in my Dropbox, so they are all in one place but with 45 repos it is very time consuming to do it manually one by one.

The solution is a bash script that will find all the repos in a directory and run git gc for each one.

The bash script (I saved it in /usr/bin/gitgc):

#!/usr/bin/env bash
 
echo "Running 'git gc' for $1"
cd "$1"
git gc --quiet

I then go to my directory of repos, and run:

find . -maxdepth 1 -name "*.git" -type d -exec gitgc "{}" ";"

This finds all directories that end in .git and calls my gitgc script for each one, passing in the directory name. If there is an error, it will be printed to the screen. The other informational messages won’t show up because of the —quiet parameter.

Modeled after a post I found here.