Housekeeping your git repository

Housekeeping a git repository is useful to make it smaller and faster. Git has a lot of tools to allow you optimizing your git repository. Let's take a look on a few of them and see what they do.


First we have to check where the "problems" are. To do this we use git-fsck:

$ git-fsck
dangling commit 65156b150f4c46f8f4bc6083afd86da9491e383b
dangling commit a7305bcd7eb44469f55b78c1913170bcd8a63e2b
dangling commit 8e35bd20b9988d8756ecee294fdbaee4c3ceb558
dangling tree 8245383436b7667d695b5599a0684be1207a7eb2
dangling blob 6c46297004f342a2cdba0b4c3fe5f63005e71091
dangling commit 437105f2a7f23899fd647e7ada683ba8dacf11ba
dangling commit 827473be18a8e1be799c91e7cdb7f8dca8688181
dangling commit 688e4e714a5ce311286de51a09b58f476eda0fdd
dangling commit f895f3f4949c15856f1d353c31c42f135cc5c599
dangling commit 75b8c133267053c9986a7c8db5131f0e7349e806
dangling blob 16bd3b210142865aa394cc866e0807b43e1e256c
dangling commit c8bf9c8b0c17ca5639046edecc93da2e62e70ded
dangling tree 27c74f04e9be4b89662324be933d9d0ffaa7f75d
dangling commit 32c82f49cc72bf020e3c0edb54d48758a46b53e3
dangling commit 48ca7e2f5a22b0c3b09c2dcc39a114b4a8847195
dangling blob bde463fef6200fcfa5af4373c525db01465243e5
dangling commit 09f345da758fca1222b0971b65b2fddbdf78bb83

Be careful, a dangling <object> can be a root node, so we should work only (for now) on unreachable objects. To get such list we issue:

$ git-fsck --unreachable
unreachable commit 9e004ebd2dab980d663784d096aa6dc449225bd8
unreachable tree c60015bd19092e936d9aa492ab6f74e10258c6ec
unreachable commit e001e796e47d29c470de6c2cd36400e03c66118b
unreachable blob d202aa9abdde172af860c67666337a4157d8fc41
unreachable commit 3f03e387893ffa07a4d5dac96772f9db3221a185
unreachable tree 85039a938e305e32744f0f5657c670a48100e9cd
unreachable tree 7104057c2987374dbb341596d88a9c23618df1ad
unreachable tree 210696bbde05adecb7f99966b382fba531260e85
unreachable tree 93073eeb7d240e9b83767fa30d8489434f0b11b8
unreachable commit ff08e657f3e7224d090dd816e00cea2c110575c4
unreachable tree 2b091f8ca730244eb1478d79d6bf77b841b36d32
unreachable commit 1f0abae87812244ad8d68034609c86129c6b8404
unreachable tree 880a427613718f7ee0e2268f910bf98bca0d1243
unreachable tree ab0c1cc1df9d88fcec61c24d76659f7ba9344ee6
unreachable tree d40c2cbb9ae5c945ce9359ca4fce279e243ad86b
unreachable commit fb0c18bd1f61086fcfac90017b59891a356cc288
unreachable tree 420dea2fd8f4caa910c4da847d854cd88d3f691f
unreachable blob a30d274b0ac67600ee5c9ef918654a2c02187e32
unreachable commit aa0ebec9cfb8a3fab966db75ac21d05a43f312a1
unreachable commit cf10e82bdc0d38d09dfaf46d0daf56136138ef3f
unreachable tree d710832406f1a69c9312fde5353381c75cf7e694
unreachable tree 2813478e966e30eb6fe6cec09ab11f16338e612e
unreachable tree ee1407c4ed3102f5eb702c0472d23c005e2e1566
unreachable commit 65156b150f4c46f8f4bc6083afd86da9491e383b
unreachable tree f7151756d523fa545480a791ec0077f0d47e0758
unreachable tree 81164ea4873d52f19cbab13e70de1130e6ed2b9a
unreachable tree 0f17eb6a771db41afcd053f287f76460487b2c15
[...]

Unreachable objects are the result of several git-branch and git-branch -D , git tries to keep objects in the repository (local) so if we recreate that branch we can get those objects back without any overhead (not quite true).

To get rid of these unreachable objects we issue: (This is perfectly safe ;-))

$ git-prune

git-prune will rm for any ocurrence of git-fsck --unreachable

Another good thing to do it running git-gc. git-gc will run some (basic) cleanups in your repository and try to optimize for performance and space utilization.

$ git-gc --aggressive

git-repack will pack unpacked objects in a repository thus optimizing space utilization. Can also be used to reorganize several packs into a single, more efficient pack.
To start repacking issue git-repack without arguments:

$ git-repack

For those who really likes performance and minimum space utilization:

$ git-repack -a

will unpack all packs and pack everything into a single pack and index file; the git-repository will be really much smaller and really much
faster, but this operation takes a long time to finish and before doing this is better issuing the command below:

$ git-prune-packed

This command will remove any unused object inside pack files.

git-pack-objects will be called by git-repack. Calling it on your own allow more control about what will be ran.
Take a look at its man page if you wanna use it ;-)

Leave a Reply