How to reduce the size of your git repo


To reduce the size of your repository let’s first look at how big is it:

$ du -sh .git
5.6M	.git

And then counting the objects in the repository:

$ git count-objects -vH
count: 181
size: 916.00 KiB
in-pack: 9817
packs: 5
size-pack: 3.64 MiB
prune-packable: 30
garbage: 0
size-garbage: 0 bytes

Then, we can use the git gc command to reduce the size:

$ git gc --aggressive
Enumerating objects: 9773, done.
Counting objects: 100% (9773/9773), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8402/8402), done.
Writing objects: 100% (9773/9773), done.
Total 9773 (delta 5522), reused 3924 (delta 0)

We’ll notice a big change when checking again the size:

$ du -sh .git
2.3M	.git

there’s also a reduction in the objects count:

$ git count-objects -vH
count: 0
size: 0 bytes
in-pack: 9773
packs: 1
size-pack: 2.11 MiB
prune-packable: 0
garbage: 0
size-garbage: 0 bytes
git