How to Perform Git Cleanup and Optimization

Published May 06, 2026

How to clean up and optimize your git repository

If your git repository is growing and you have gigabytes of objects, this blog post will show you how to clean it up, as in review and delete the extra stuff in there.

Check disk space of git repository

Get into your local git repository and check the disk space.

You can run this command to list the directories in sorted order. The largest directory shows up last:

du -h | sort -h

In my case, I had a few directories with gigabytes of data being used.

git count-objects -vH

We will use git count-objects -vH to see how many git objects we have.

git count-objects -vH inspects your repository’s object database (what is under .git/objects) and prints a human‑readable, verbose summary of how many git objects you have and how much disk space they use.

The output will end with something like this:

count: 6489
size: 122.35 MiB
in-pack: 431
packs: 1
size-pack: 2.12 MiB
prune-packable: 0
garbage: XX
size-garbage: XX bytes

where XX is the garbage size in bytes. You can get rid of it in the next step.

git gc

Next, we will run this command to clean up our git repo.

git gc stands for garbage collection. It cleans up and optimizes your git repository to reduce disk usage and improve performance.

git gc

When you run that command:

  • the .git folder becomes smaller
  • commands like git status and git log become faster
  • loose objects and garbage reported by git count-objects -vH are cleaned up

git gc --aggressive

To do cleanup at a more intensive level, run this:

git gc --aggressive

That command recomputes deltas as well, and results in a smaller repository size.

Verify disk space of git repo

Run this again to verify the newly updated disk space:

du -h | sort -h

You will observe that the key size-garbage will most likely show 0 bytes.

Conclusion

If these git cleanup commands have worked or not worked for you, let me know via email or add a comment below. Thanks for reading.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: May 06, 2026.     This post was originally written on May 05, 2026.