Optimise local repositories with Git Garbage Collection

TIL how to optimise local repositories with Git Garbage Collection.

Working with git is awesome. Just do not forget that if you work a lot with git, a lot of garbage is getting piled up in your local repository, such as file revisions and other objects. To reduce disk space and increase performance, git provides a command to compress file revisions and remove unreachable objects:

$ git gc
Counting objects: 1278, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (1068/1068), done.
Writing objects: 100% (1278/1278), done.
Total 1278 (delta 234), reused 1096 (delta 102)

Is it safe to run?

git gc will only optimise your local repository. The remote repository is safe! Note that some git commands may automatically run git gc.

Every few hundred changesets or so

git gc runs very quickly. However, every hundred changesets or so you need a deeper cleaning. This can be accomplished with the --aggressive option. Depending on the size of your repository, this can take much more time, but provides better results.

Learn more

With git --help you can learn more or read the documentation here.