How to clean up the git repo and reduce its disk size
Tags: git, beginners, tutorial, productivity
Published: 2020-08-03 14:23:44

---
title: How to clean up the git repo and reduce its disk size
published: true
tags: git, beginners, tutorial, productivity
canonical_url: https://gitbetter.substack.com/p/how-to-clean-up-the-git-repo-and
---

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,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)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

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
```

#### Deleting local reference of the remote branch

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
```

#### git repack

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 prune-packed

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 reflog expire

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.

#### git gc

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.

#### **Combining all command**

```
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.

Thank you :)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.