Cleaning a git repo

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.

Any object that is unreachable from a stored reference are deleted. If you’ve got a large repository and lots of rebasing going on, this will likely get large.

Running git gc will remove your reflog history, which you might not want to do. So be sure you’re happy to lose that history.