Since we use git numerous times a day over and over, hence needs maintenance. There are few commands that will help you to keep our git healthy. If you feel that you git has somewhat slowed down, these would do the trick for you.
Since ours is a pretty big project . I run it once a week to make sure my git is healthy.
This commands helps to identify any corrupted, unreachable (dangling blobs) objects in your database.
git fsck dangling commit 652424f02c17b5d881158a625f1a34f6039dc231 dangling commit 7e6510c2663530d949ea9fb67900496dcf5cbc4c dangling commit 177864469ea87564ade9101106d5aa85cceb0d29 dangling commit 848665007bc412910b68dc3e528e74c23ce1f165 dangling blob dfa6756511b525996946de84fa7f67b6f9a0d32f dangling blob e7f8f1145c7e739a6ffd8806a6d26d60f0443897 dangling blob f1f905a5eb35724622249f25f6d9ef7c646e4283 dangling blob 221417781e017cf02f90b872aea80f30ed41787c dangling blob c1356b24fb2a4ea453dba87ed4058e7a48e97dce dangling blob cb6fe3d5321b7b152a8781541bbc6132c59b9c94 dangling blob 3ce2e76cd54f0c8265ec6167f78d5dfe71c2242d dangling blob daf073729960c40a44c5291c745092445bb2c3b0
With git fsck, we identified some of dangling commits and blobs, now to remove them we use git prune.
Now After running git prune, If I run git fsck again, my git would be cleaner than before and healthy.
git fsck dangling commit 177864469ea87564ade9101106d5aa85cceb0d29 dangling commit 848665007bc412910b68dc3e528e74c23ce1f165
But it still has some dangling commits.This is where git’s garbage collection comes in play.
3.git gc (Garbage Collection)
This command will gathers up all the loose objects and places them in packfiles. And removes objects that aren’t reachable from any commit. This command will save some space and speed up many of git commands.
git gc Counting objects: 121775, done. Delta compression using up to 4 threads. Compressing objects: 100% (18239/18239), done. Writing objects: 100% (121775/121775), done. Total 121775 (delta 76189), reused 120913 (delta 75659) Removing duplicate objects: 100% (256/256), done.
Output of this commands tells several things to us , that is compressed 18239 objects for us out of total 121775 objects. And it also removed 256 duplicate objects. This way my git stayed healthy.