Bird
Raised Fist0
Gitdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the git gc command?
easy
A. To create a new branch in the repository
B. To clean up unnecessary files and optimize the repository
C. To clone a remote repository locally
D. To merge two branches together

Solution

  1. Step 1: Understand the function of git gc

    The git gc command stands for garbage collection and is used to clean up unnecessary files in a Git repository.
  2. Step 2: Identify the main effect of running git gc

    Running git gc reduces repository size and improves performance by removing unneeded files.
  3. Final Answer:

    To clean up unnecessary files and optimize the repository -> Option B
  4. Quick Check:

    Garbage collection = Clean up files [OK]
Hint: Remember: gc means garbage collection, cleans repo files [OK]
Common Mistakes:
  • Confusing git gc with branch creation
  • Thinking it clones repositories
  • Assuming it merges branches
2. Which of the following is the correct syntax to run an aggressive garbage collection in Git?
easy
A. git gc --aggressive
B. git gc --fast
C. git gc -a
D. git gc --deep-clean

Solution

  1. Step 1: Recall the option for deeper cleanup

    The --aggressive option tells Git to perform a more thorough garbage collection.
  2. Step 2: Match the correct syntax

    The correct command is git gc --aggressive. Other options like --fast or --deep-clean do not exist.
  3. Final Answer:

    git gc --aggressive -> Option A
  4. Quick Check:

    Deeper cleanup = --aggressive [OK]
Hint: Use --aggressive for deep cleanup, not --fast [OK]
Common Mistakes:
  • Using non-existent flags like --deep-clean
  • Confusing -a with --aggressive
  • Assuming --fast does deep cleanup
3. What will be the effect of running git gc on a repository with many unreachable objects?
medium
A. It will reset the repository to the last commit
B. It will create new unreachable objects
C. It will delete unreachable objects and compress repository data
D. It will only update branch pointers

Solution

  1. Step 1: Understand unreachable objects in Git

    Unreachable objects are files or commits not referenced by any branch or tag and can be safely removed.
  2. Step 2: Effect of git gc on unreachable objects

    Running git gc deletes these unreachable objects and compresses data to optimize the repository.
  3. Final Answer:

    It will delete unreachable objects and compress repository data -> Option C
  4. Quick Check:

    Garbage collection removes unreachable objects [OK]
Hint: Garbage collection removes unneeded data safely [OK]
Common Mistakes:
  • Thinking it creates unreachable objects
  • Confusing it with reset commands
  • Assuming it only updates pointers
4. You ran git gc --aggressive but noticed the repository size did not reduce much. What could be a possible reason?
medium
A. Git does not support aggressive garbage collection
B. The command syntax was incorrect
C. The repository is corrupted
D. There are many recent commits and few unreachable objects

Solution

  1. Step 1: Understand when garbage collection reduces size

    Garbage collection mainly removes unreachable objects and compresses data. If most data is reachable, size won't reduce much.
  2. Step 2: Analyze the scenario

    If the repository has many recent commits and few unreachable objects, git gc --aggressive will have little effect on size.
  3. Final Answer:

    There are many recent commits and few unreachable objects -> Option D
  4. Quick Check:

    Few unreachable objects = little size change [OK]
Hint: Aggressive gc helps only if unreachable objects exist [OK]
Common Mistakes:
  • Assuming syntax error without checking
  • Believing aggressive gc is unsupported
  • Jumping to repository corruption conclusion
5. You want to reduce your Git repository size significantly and improve performance. Which sequence of commands should you run?
hard
A. git gc --aggressive; git prune; git gc
B. git gc; git gc --aggressive; git prune
C. git prune; git gc --aggressive; git gc
D. git fetch; git gc --aggressive; git prune

Solution

  1. Step 1: Understand the role of each command

    git gc --aggressive performs deep cleanup, git prune removes unreachable objects, and running git gc again optimizes after pruning.
  2. Step 2: Determine the best order

    First run git gc --aggressive for deep cleanup, then git prune to remove unreachable objects, and finally git gc to optimize the repository fully.
  3. Final Answer:

    git gc --aggressive; git prune; git gc -> Option A
  4. Quick Check:

    Deep cleanup + prune + optimize = D [OK]
Hint: Run aggressive gc, then prune, then gc again for best cleanup [OK]
Common Mistakes:
  • Running prune before aggressive gc
  • Skipping final gc optimization
  • Confusing fetch with garbage collection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard