Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active July 18, 2025 05:48
Show Gist options
  • Save magnetikonline/4064851 to your computer and use it in GitHub Desktop.
Save magnetikonline/4064851 to your computer and use it in GitHub Desktop.
Recursively garbage collect (compress) all child Git repositories from a given base directory.
#!/bin/bash -e
function main {
if [[ ! -d $1 ]]; then
echo "Usage: $(basename "$0") DIR" >&2
exit 1
fi
local IFS=$'\n'
local gitRepoDir
for gitRepoDir in $(find -L "$1" -type d -name ".git"); do
gitRepoDir=${gitRepoDir%/.git}
echo "$gitRepoDir"
git \
--git-dir "$gitRepoDir/.git" gc \
--aggressive \
--prune=now
echo
done
}
main "${1%/}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment