← Back to Git Course | Chapter 10: Tools & Internals | Lesson 5 of 8

Git gc और Maintenance

git gc एक spring cleaning जैसा है जो loose papers को neat bundles में pack कर देता है ताकि project folder छोटा और quick रहे।
Syntax
bash
git count-objects -v
git gc [--prune=now]

Git Garbage Collection क्या है?

जैसे-जैसे आप files modify और delete करते हैं, Git loose, unreferenced objects और legacy history fragments जमा करता है। Garbage collection इन files को packed files में compress करता है, repository size और speed optimize करते हुए।

उदाहरण: What is Git Garbage Collection?

bash
git count-objects -v

git gc चलाना

git gc loose objects को compressed pack files में bundle करता है और अब किसी से referenced न होने वाले objects हटाता है, .git directory छोटा करते हुए और history scan करने वाले operations को तेज़ करते हुए — ज़्यादातर repositories को इसे manually चलाने की कभी ज़रूरत नहीं क्योंकि Git कुछ thresholds के बाद automatically इसे trigger करता है।

उदाहरण: Running git gc

bash
git gc

Loose Objects को Pruning करना

एक discarded commit, एक aborted rebase, या एक deleted branch से बचे objects तब तक रहते हैं जब तक वे pruned न हों — git prune किसी भी branch, tag, या reflog से अब reachable न होने वाला कोई भी object हटा देता है, उनके इस्तेमाल की गई disk space free करते हुए।

उदाहरण: Pruning Loose Objects

bash
git prune

Repository Health Check करना

git fsck repository के database में हर object corruption या dangling references (ऐसे objects जो exist करते हैं लेकिन कोई उन्हें point नहीं करता) के लिए check करते हुए चलता है, जो वह tool है जिस तक पहुँचना है अगर एक repository अजीब behave करना शुरू करे और आपको लगे कि underlying data खुद damaged है।

उदाहरण: Checking Repository Health

bash
git fsck

Automated Background Maintenance

Git 2.30 से, git maintenance background tasks — prefetching, incremental repacking, loose-object cleanup — को periodically चलने के लिए schedule कर सकता है बिना आपको खुद gc invoke करना याद रखे, बड़ी, long-lived repositories को manual upkeep के बिना तेज़ रखते हुए।

उदाहरण: Automated Background Maintenance

bash
git maintenance start
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. git gc --prune=now चलाना और recoverable commits रखने की उम्मीद करना, जबकि यह unreferenced objects permanently हटा देता है।
  2. habit से अक्सर git gc चलाना, जबकि Git ज़रूरत पड़ने पर पहले से automatically इसे चलाता है।
  3. बड़े binaries commit करने के बाद git gc से repository के काफी छोटा होने की उम्मीद करना, जबकि files history में रहती हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.