Free and optimize GIT disk space
Usually, these days storage devices have evolved, and we no longer care about how much space a file or a program needs on the disk. But, in some particular cases (like virtual machines or docker containers), we might face this problem.
Recently I faced an issue on a virtual machine. One of the VM that I am using does not have much storage space, but I use it for small jobs, including pull/push commands in some git repos. With time, the size and the number of files used by git increased and I landed up with a very few disk space.
In order to optimize and cleanup unnecessary files from your local repository, git has a command:
git gc [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force] [--keep-largest-pack]
This command will compress file version and remove unreachable objects. It also pack refs, prune reflog etc in order to reduce the disk space used and increase the performance. More about git gc
command can be found here.
Using this command will result in reducing the disk space used, and increase the performance, but there are cases when you land up with a 100% disk usage, and in this case git gc
won’t help you, because it needs additional disk space to run the tasks.
In this case, you can use another git command: git prune
.
git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>…]
This command will prune all unreachable objects from the object database. More about this command can be found here.
In my particular case, I had 100% full disk space, so first I used:
git prune
in order to prune the unreachable objects, and then
git gc --prune=all
--prune=all
argument prune loose objects.