Back to Git & Dev Tools
2026-01-218 min read

git-count-objects[1] (Git & Dev Tools)

Learn git-count-objects[1] (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In this guide, we'll delve into the Git command git count-objects, a powerful tool every developer should be familiar with to manage their repository efficiently and troubleshoot common issues. By understanding how to use git count-objects, you can optimize your Git repository for better performance, reduce disk usage, and make informed decisions about when to repack it. Additionally, this command helps in debugging common issues with Git repositories, making it an essential tool for developers working on large projects or collaborating with others.

Prerequisites

To follow this guide, you should have a basic understanding of Git and its fundamental commands such as git init, git add, git commit, and git push. Familiarity with the Git directory structure and how objects are stored in a Git repository is also beneficial but not strictly necessary. It's recommended to have a Git repository set up for practicing the examples provided in this guide.

Core Concept

Overview

Git count-objects is a command that calculates the number of unpacked object files and disk space consumed by them in your Git repository. It helps you monitor the size of your repository, which can be useful when working on large projects or collaborating with others. By periodically checking the size of your repository, you can make informed decisions about when to repack it, which can significantly improve performance and reduce disk usage.

Syntax

The basic syntax for using git count-objects is:

git count-objects [-v] [-H | --human-readable]

Options

  • -v, --verbose: Provides more detailed reports, including the number of loose objects, in-pack objects, pack files, and disk space consumed by each. When combined with -H or --human-readable, it displays sizes in a human-readable format (e.g., KB, MB).

How It Works

When you run git count-objects, Git scans your repository for objects and provides information about the number of loose objects, in-pack objects, pack files, and disk space consumed by each category. Loose objects are individual object files that reside in the .git/objects directory, while in-pack objects are stored within pack files (.pack, .idx, and .shard) within the same directory.

Prune-Packable Objects

Git count-objects also reports the number of loose objects that are also present in the packs. These objects could be pruned using git prune-packed, which removes loose objects that can be found in existing pack files to save disk space.

Garbage Files

In addition to object files and pack files, Git count-objects reports the number of garbage files (.git/garbage) in your repository. These files are neither valid loose objects nor valid packs and can be safely removed to reclaim disk space.

Example Repository Structure

To better understand how Git stores objects, let's take a look at an example repository structure:

my_project/
├── .git/
│ ├── objects/
│ │ ├── 0a/
│ │ ├── 0b/
│ │ ├── 0c/
│ │ ├── ...
│ │ └── pack/
│ │ ├── pack-1.pack
│ │ ├── pack-1.idx
│ │ ├── pack-2.pack
│ │ ├── pack-2.idx
│ │ └── ...
│ ├── refs/
│ │ ├── heads/
│ │ │ ├── master
│ │ │ └── ...
│ │ └── tags/
│ │ ├── v1.0.0
│ │ ├── v1.1.0
│ │ └── ...
│ └── ...
├── README.md
├── app.js
├── package.json
└── ...

In this example, the repository contains a my_project directory with project files (e.g., README.md, app.js, and package.json) and a hidden .git directory containing Git-specific files such as objects, packs, refs, and other metadata.

Worked Example

In this example, we'll demonstrate how to use git count-objects to monitor the size of a Git repository and make informed decisions about when to repack it.

First, navigate to your project directory:

cd my_project

Next, run git count-objects without any options to get an overview of the number of objects and disk space consumed by them:

git count-object

The output will look something like this:

Counting objects: 1000, done.
Delta compression using up to 8 threads.
Compressing objects: 100%, done.
Writing objects: 100%, done.
Total 1000 (delta 25), reused 0 (delta 0)

In this example, Git reports that there are 1000 objects in the repository, with 25 of them being delta compressed (i.e., changes to existing objects rather than new objects). No disk space consumption is shown because we didn't use the -H or --human-readable options.

Now let's run git count-objects with the -v and -H options to get a more detailed report:

git count-object -v -H

The output will look something like this:

Counting objects: 1000, done.
Delta compression using up to 8 threads.
Compressing objects: 100%, done.
Writing objects: 100%, done.
Total 1000 (delta 25), reused 0 (delta 0)
size/count blob tree commit tag
-------- ----- ---- ------- ----
348K 796 0 0 0
184K 195 0 0 0
28K 1 0 0 0
0 0 0 1 0
0 0 0 0 0

In this example, Git provides a breakdown of the number and size (in human-readable format) of each object type: blobs (individual files), trees (directory structures), commits, tags, and garbage. The total number of objects remains the same as before, but we now have more detailed information about their sizes and types.

Now let's check for prune-packable objects by running git prune:

git prune

Git will remove any loose objects that are also present in pack files, saving disk space. The output will look something like this:

Counting objects: 1000, done.
Delta compression using up to 8 threads.
Compressing objects: 100%, done.
Writing objects: 100%, done.
Total 975 (delta 24), reused 0 (delta 0)

In this example, Git removed 25 loose objects that were also present in pack files, reducing the total number of objects to 975.

Common Mistakes

1. Not Repacking Regularly

One common mistake is not repacking your Git repository regularly to optimize its performance and reduce disk usage. By periodically running git count-objects and pruning packable objects using git prune-packed, you can keep your repository lean and efficient.

2. Misinterpreting Object Counts

Another common mistake is misinterpreting the object counts reported by git count-objects. It's essential to understand that Git stores objects in both loose and packed formats, so the number of objects may appear higher than expected due to duplicate storage.

3. Not Understanding Delta Compression

Delta compression can be confusing for some developers because it reduces the number of objects reported by git count-objects but does not necessarily mean that less disk space is being used. It's essential to understand how delta compression works and monitor both the number of objects and their total size when optimizing your Git repository.

4. Not Using git prune Correctly

When using git prune, it's important to be aware that it only removes loose objects that are also present in pack files. If you have loose objects that are not in packs (e.g., unreferenced objects), running git prune will not affect them. To remove these objects, you can use git gc or manually delete them from the .git/objects directory.

Practice Questions

  1. What is the purpose of git count-objects, and why is it important for developers?
  2. How can you use git count-objects to make informed decisions about when to repack your Git repository?
  3. What does the -v and -H options do in git count-objects, and how do they affect the output?
  4. Explain the difference between loose objects and pack files in a Git repository.
  5. How does delta compression work in Git, and why is it important?
  6. What are some common mistakes when using git count-objects, and how can you avoid them?
  7. When should you use git prune, git gc, or manually deleting objects from the .git/objects directory to optimize your Git repository?
  8. How can you check for unreferenced objects in your Git repository?

FAQ

1. How often should I run git count-objects to monitor my Git repository's size?

It depends on the size of your project and how actively you are working on it. A good rule of thumb is to run git count-objects every few hundred commits or whenever you notice a significant increase in disk usage.

2. What is the difference between loose objects and pack files, and why does Git store objects in both formats?

Loose objects are individual object files that reside in the .git/objects directory, while pack files (.pack, .idx, and .shard) contain multiple objects compressed together to save disk space. Git stores objects in both loose and packed formats for flexibility and performance: loose objects can be accessed quickly when needed, while pack files help reduce disk usage by storing multiple objects in a single file.

3. How does delta compression work in Git, and why is it important?

Delta compression in Git works by comparing changes between two similar objects (e.g., consecutive commits) and storing only the differences instead of the entire object. This reduces the number of objects stored in the repository, saving disk space and improving performance. Delta compression is essential for large projects with many small changes, as it can significantly reduce the size of the Git repository.

4. When should you use git prune, git gc, or manually deleting objects from the .git/objects directory to optimize your Git repository?

  • Use git prune when you want to remove loose objects that are also present in pack files to save disk space.
  • Use git gc (garbage collection) when you want to clean up unreferenced objects, garbage files, and other unnecessary data from your Git repository. This command can be run with the --aggressive option to remove loose objects that are not in packs as well.
  • Manually deleting objects from the .git/objects directory should be done with caution, as it can potentially cause data loss if not done correctly. It's recommended to use git prune and git gc instead, or consult Git documentation for safe manual object removal procedures.

5. How can you check for unreferenced objects in your Git repository?

To check for unreferenced objects in your Git repository, run the following command:

git fsck --unreachable --dangling

This command will list any unreferenced objects that are not being used by your repository. Be cautious when removing these objects, as they may be needed for the proper functioning of your Git repository.

git-count-objects[1] (Git & Dev Tools) | Git & Dev Tools | XQA Learn