• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

git gc: Optimise the local repository by cleaning unnecessary files

by admin

“git gc” is a Git command that stands for “garbage collection.” It is used to optimize and clean up a local Git repository by removing unnecessary files and optimizing storage.

Here’s how “git gc” works:

  • Garbage Collection: Git uses a unique data structure called the “Git object database” to store files, commits, trees, and other objects. Over time, as you make changes to your repository, new objects are created, and some objects become obsolete or unreferenced. These unreferenced objects are considered “garbage” because they are no longer needed.
  • Removing Unreferenced Objects: When you run “git gc,” it performs a garbage collection process. It identifies unreferenced objects in the repository and removes them, freeing up disk space. This helps optimize the storage utilization of the repository.
  • Compression and Consolidation: In addition to removing unreferenced objects, “git gc” also performs compression and consolidation of Git objects. It optimizes the storage by combining multiple small Git objects into larger ones and compressing the object data to reduce the overall repository size.
  • Housekeeping: “git gc” also performs various housekeeping tasks, such as cleaning up temporary files, optimizing internal data structures, and updating reference pointers. These tasks ensure the repository’s integrity and improve its overall performance.

Running “git gc” periodically is recommended to keep your Git repository efficient and optimize its performance. It helps reclaim disk space by removing unnecessary objects and improves the speed of various Git operations, such as fetching, cloning, and pushing.

It’s important to note that “git gc” is an automatic process that Git performs periodically in the background. However, you can also manually trigger it using the command to optimize the repository at any time.

git gc Command Examples

1. Optimise the repository:

# git gc

2. Aggressively optimise, takes more time:

# git gc --aggressive

3. Do not prune loose objects (prunes by default):

# git gc --no-prune

4. Suppress all output:

# git gc --quiet

5. View full usage:

# git gc --help

Summary

In summary, “git gc” is a command used to optimize and clean up a local Git repository. It removes unreferenced objects, compresses and consolidates Git objects, and performs housekeeping tasks to improve the repository’s efficiency, reclaim disk space, and enhance overall performance.

Filed Under: Linux

Some more articles you might also be interested in …

  1. git archive: Create an archive of files from a named tree
  2. env: command not found
  3. “docker ps” Command Examples
  4. initctl Command Examples in Linux
  5. runcon Command Examples in Linux
  6. atoum: A simple, modern and intuitive unit testing framework for PHP (Command Examples)
  7. ufw Command Options
  8. brittany: Pretty-print Haskell source files
  9. asar – A file archiver for the Electron platform (Command Examples)
  10. How to Setup a sudo Switch to Another User That Has no Password or ssh Key Set in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright