Cleaning up local git repos

We often get to a point where we have billions of local git repositories that have been merged already, or abandoned - it’s often difficult to see the wood from the trees.

Prune remote tracking branches

git remote prune origin

This only deletes the remote references to these branches, if they have been deleted on origin - it doesn’t delete them locally. It does however clean-up views you get of the current code, and removes some git cruft locally.

Delete local branches that have been merged

git branch --merged >/tmp/merged-git-branches
  && vi /tmp/merged-git-branches
  && xargs git branch -d >/tmp/merged-git-branches

This command lists your merged branches in Vim, and allows you to remove the ones you wish to keep (notably master).

Save and exit that temp file to then locally prune all the merged branches.

Garbage collect loose ends

git gc

Read more about git gc here. A simple run through of any unreferenced objects and unnecessary files to speed up merges and pushes. You’re supposed to run this ‘regularly’, apparently…