Recursively clean up local git repositories

  1. Intro
  2. Howto
  3. Note

Intro

If you’re like me and have hundreds of local git repositories on your drive it is a real pain to keep them all compact. So time for another neat CLI command.

Howto

Add the lines below to ~/bin/gitgc (and chmod +x it) or whichever you prefer and simply run gitgc to recurse all directories from your current path and automagically garbage collect all repositories it finds.

#!/bin/bash
export arg=$@
find . -name '.git' -type d -exec bash -c 'path=${1%/.git}; echo $path; cd "$path" && git gc $arg' 0 {} \;

It even accepts commandline options, so you can do gitgc --aggressive to really clean up your repos.

Note

Tested on Ubuntu Server 14.04 and OSX 10.9.3.

git gc documentation


Related stuff

Leave a Comment

Your email address will not be published. Required fields are marked *