0
0
Gitdevops~10 mins

Garbage collection with git gc - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Garbage collection with git gc
Start: git repository
Run git gc command
Scan objects in .git/objects
Identify unreachable objects
Compress reachable objects
Remove unreachable objects
Update repository state
Finish: optimized repository
The git gc command scans the repository, compresses reachable objects, removes unreachable ones, and updates the repository to optimize storage.
Execution Sample
Git
git gc
# Runs garbage collection to clean and optimize the repo
This command cleans up unnecessary files and optimizes the local git repository.
Process Table
StepActionDetailsResult
1Start git gcBegin garbage collection processReady to scan objects
2Scan objectsLook through .git/objects folderList of all objects found
3Identify unreachable objectsFind objects not referenced by any branch, tag, or stashUnreachable objects marked for removal
4Compress reachable objectsPack loose objects into packfilesStorage optimized
5Remove unreachable objectsDelete objects no longer neededDisk space freed
6Update repository stateUpdate refs and packfilesRepository optimized
7FinishGarbage collection completeRepository ready for use
💡 All unreachable objects removed and reachable objects compressed, git gc process ends
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Objects in repoAll objects (loose + packed)All objects listedReachable + unreachable identifiedReachable compressedUnreachable removedOptimized set of objects
Key Moments - 2 Insights
Why does git gc remove some objects but not others?
git gc only removes unreachable objects, which are not referenced by any branch, tag, or stash. Reachable objects are kept and compressed for efficiency, as shown in steps 3 and 5 of the execution table.
What does compressing objects mean in git gc?
Compressing means packing many small loose objects into fewer packfiles to save space and improve performance. This happens in step 4, reducing storage size without losing data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step are unreachable objects removed?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Check the 'Action' column for removal of unreachable objects in the execution table.
According to the variable tracker, what happens to the objects after step 4?
AAll objects are removed
BUnreachable objects are listed
CReachable objects are compressed
DRepository state is updated
💡 Hint
Look at the 'After Step 4' column in the variable tracker for objects in repo.
If no unreachable objects exist, which step would have no effect?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Refer to the execution table where unreachable objects are removed.
Concept Snapshot
git gc runs garbage collection in a git repo
It scans all objects and identifies unreachable ones
Unreachable objects are deleted to free space
Reachable objects are compressed into packfiles
This optimizes storage and speeds up git operations
Full Transcript
The git gc command starts by scanning all objects in the repository's .git/objects folder. It identifies which objects are reachable, meaning they are referenced by branches, tags, or stashes, and which are unreachable. Unreachable objects are those no longer needed and safe to remove. Then, git gc compresses the reachable objects by packing them into fewer files to save space. After removing unreachable objects and compressing reachable ones, git gc updates the repository state to reflect these changes. This process frees disk space and improves repository performance. The execution table shows each step clearly, and the variable tracker follows how the objects change during the process.