git-gc:tldr:8d3fc
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:
-
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. -
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. -
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. -
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.