Forrest logo
back to the git tool

git-gc:tldr:8d3fc

git-gc: Optimise the repository.
$ git gc
try on your machine

The git gc command stands for "git garbage collector." It is a command used in Git to perform housekeeping operations and optimize the repository by cleaning up unnecessary files and improving performance.

When you run git gc, Git performs several tasks:

  1. It compresses loose objects: Git stores file revisions as objects, and sometimes these objects become fragmented or duplicated due to various actions such as committing, amending, or rebasing. git gc compresses these objects and removes the unnecessary ones, reclaiming disk space.

  2. It packs loose objects into packs: Git bundles objects into pack files to optimize storage and reduce overhead. git gc packs the loose objects within a repository into pack files, making the repository more efficient.

  3. It removes reflogs that are no longer reachable: Git keeps a reflog for each branch, which stores a history of its previous states. Over time, reflogs can become quite large and take up unnecessary space. git gc removes any reflogs that are no longer reachable, freeing up disk space.

  4. It prunes old unreachable objects: Sometimes, objects in Git become unreachable, possibly due to branch deletion or history rewriting (e.g., using git filter-branch). These unreachable objects are no longer needed and can be safely deleted. git gc identifies and removes such unreachable objects, further optimizing the repository.

Overall, running git gc helps keep the Git repository clean, compact, and performant. It is generally not necessary to run this command manually as Git automatically performs garbage collection in the background when needed.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the git tool