Skip to content

Instantly share code, notes, and snippets.

@phette23
Created January 22, 2026 19:51
Show Gist options
  • Select an option

  • Save phette23/0f331bf18340d5622b5d4c2e60ecb89b to your computer and use it in GitHub Desktop.

Select an option

Save phette23/0f331bf18340d5622b5d4c2e60ecb89b to your computer and use it in GitHub Desktop.
recursive git garbage collection

Recursive Git Garbage Collection

This can save some disk space, especially if you have many large git repos on your hard drive. Meant to be run from a parent directory where all code/projects are kept. Skips directories that aren't git repos.

#!/usr/bin/env python
# Recursively runs 'git gc' in all git repositories starting from the current directory.
import subprocess
from os import walk
for dirpath, dirnames, filenames in walk("."):
if ".git" in dirnames:
print(f"Running 'git gc' in {dirpath}")
subprocess.run(["git", "-C", dirpath, "gc"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment