This quote is taken from;
Version Control with Git
Git runs garbage collection automatically:
• If there are too many loose objects in the repository
• When a push to a remote repository happens
• After some commands that might introduce many loose objects
• When some commands such as git reflog expire explicitly request it
And finally, garbage collection occurs when you explicitly request it
using the git gc command. But when should that be? There’s no solid
answer to this question, but there is some good advice and best
practice.
You should consider running git gc manually in a few
situations:
• If you have just completed a git filter-branch . Recall that
filter-branch rewrites many commits, introduces new ones, and leaves
the old ones on a ref that should be removed when you are satisfied
with the results. All those dead objects (that are no longer
referenced since you just removed the one ref pointing to them)
should be removed via garbage collection.
• After some commands that might introduce many loose objects. This
might be a large rebase effort, for example.
And on the flip side,
when should you be wary of garbage collection?
• If there are orphaned refs that you might want to recover
• In the context of git rerere and you do not need to save the
resolutions forever
• In the context of only tags and branches being sufficient to cause
Git to retain a commit permanently
• In the context of FETCH_HEAD retrievals (URL-direct retrievals via
git fetch ) because they are immediately subject to garbage collection