---
title: How to clean up the git repo and reduce its disk size
updated: 2024-06-08 10:33:24Z
created: 2024-06-08 10:33:24Z
source: https://gitbetter.substack.com/p/how-to-clean-up-the-git-repo-and
---

> Hi there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.

[![](../_resources/https_3A_2F_2Fbucketeer-e05bbc84_0d6d07da9c32469cb.gif)](https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2d00ec-94e8-4a42-934e-3c7fd99177e8_800x600.gif)

If you are working in a Git repo for a very long time then you can cleanup your repo to gain disk space.

Git has an internal garbage collection tool that takes care of most of the things but there are few things that we can also do to clean up the repo.

Let’s see some techniques to clean up the repo.

Before applying these techniques to git, make sure you get the size of the **.git** directory using

```
du -sh .git
```

It’s always a good practice to delete a branch after it is merged. Github provides an option to delete the branch once you merged the PR. But this one will delete that branch only in the remote.

Even after the branch is deleted in the remote, it will still have the reference in the local.

To delete all the local references of the remote branch

```
git remote prune origin
```

Packs are Git internal representations that used to combine all individual objects into packs.

Without going much deeper, Packs are used to reduce the load on disk spaces, mirror systems, etc.

```
git repack
```

This will create new packs that are not packed yet in the repo. This helps in reducing disk sizes.

Git will have some pack files. This command will help you to reduce extra objects that are already present in the pack files. This will help you to reduce the size of the pack file itself.

```
git prune-packed
```

Git has a feature called reflog that helps to track Git refs in the local repo. Git has an internal garbage collection mechanism to remove old refs in Git. But there is also a manual mechanism to remove old refs.

```
git reflog expire --expire=1.month.ago
```

The above command will remove all refs that are older than one month. I think one month is safer. But if you can mention whatever value you feel safe.

gc stands for garbage collection. This command will help Git to remove unwanted data in the current repo.

```
git gc --aggressive
```

The above command will remove all refs and inaccessible commits in the repo which are older than two weeks. **—aggressive** will help more time optimizing it.

```
git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
```

We have combined all the commands. You can have it as an alias and run it weekly once to have a clean repo.

And don’t forget to see the .git directory size after cleaning up your repo.

Running it across all the repos in your machine will definitely help you reduce some disk space.

That’s it for today :)

See you next week :) :)

### Subscribe to Git Better

Get better with Git. Tips, tricks and advanced topics of Git