<p>How often should you use git-gc?  </p>

<p>The <a href="http://www.kernel.org/pub/software/scm/git/docs/git-gc.html" rel="noreferrer">manual page</a> simply says:</p>

<blockquote>
  <p>Users are encouraged to run this task on a regular basis within each repository to maintain good disk space utilization and good operating performance.</p>
</blockquote>

<p>Are there some commands to get some object counts to find out whether it's time to gc?</p>

[{'answer_id': 55738, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 9, 'selected': True, 'text': "<p>It depends mostly on how much the repository is used.  With one user checking in once a day and a branch/merge/etc operation once a week you probably don't need to run it more than once a year.</p>\n\n<p>With several dozen developers working on several dozen projects each checking in 2-3 times a day, you might want to run it nightly.</p>\n\n<p>It won't hurt to run it more frequently than needed, though.</p>\n\n<p>What I'd do is run it now, then a week from now take a measurement of disk utilization, run it again, and measure disk utilization again.  If it drops 5% in size, then run it once a week.  If it drops more, then run it more frequently.  If it drops less, then run it less frequently.</p>\n"}, {'answer_id': 55789, 'author': 'Pat Notz', 'author_id': 825, 'author_profile': 'https://Stackoverflow.com/users/825', 'pm_score': 3, 'selected': False, 'text': "<p>Drop it in a cron job that runs every night (afternoon?) when you're sleeping.</p>\n"}, {'answer_id': 67054, 'author': 'Amandasaurus', 'author_id': 161922, 'author_profile': 'https://Stackoverflow.com/users/161922', 'pm_score': 3, 'selected': False, 'text': '<p>I use git gc after I do a big checkout, and have a lot of new object. it can save space. E.g. if you checkout a big SVN project using git-svn, and do a git gc, you typically save a lot of space</p>\n'}, {'answer_id': 67108, 'author': 'mrowe', 'author_id': 3913, 'author_profile': 'https://Stackoverflow.com/users/3913', 'pm_score': 5, 'selected': False, 'text': '<p>Recent versions of git run gc automatically when required, so you shouldn\'t have to do anything. See the Options section of <a href="http://www.kernel.org/pub/software/scm/git/docs/git-gc.html#_options" rel="noreferrer">man git-gc(1)</a>: "Some git commands run git gc --auto after performing operations that could create many loose objects."</p>\n'}, {'answer_id': 87445, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 7, 'selected': False, 'text': '<p>Note that the downside of garbage-collecting your repository is that, well, the garbage gets collected. As we all know as computer users, files we consider garbage right now might turn out to be very valuable three days in the future. The fact that git keeps most of its debris around has saved my bacon several times – by browsing all the dangling commits, I have recovered much work that I had accidentally canned.</p>\n\n<p>So don’t be too much of a neat freak in your private clones. There’s little need for it.</p>\n\n<p>OTOH, the value of data recoverability is questionable for repos used mainly as remotes, eg. the place all the devs push to and/or pulled from. There, it might be sensible to kick off a GC run and a repacking frequently.</p>\n'}, {'answer_id': 18541285, 'author': 'cregox', 'author_id': 274502, 'author_profile': 'https://Stackoverflow.com/users/274502', 'pm_score': 4, 'selected': False, 'text': '<p>If you\'re using <a href="http://git-scm.com/docs/git-gui" rel="noreferrer"><strong>Git-Gui</strong></a>, it <a href="https://stackoverflow.com/questions/1106529/how-to-skip-loose-object-popup-when-running-git-gui">tells you</a> when you should worry:</p>\n\n<blockquote>\n<pre><code>This repository currently has approximately 1500 loose objects.\n</code></pre>\n</blockquote>\n\n<p>The following command will bring a similar number:</p>\n\n<pre><code>$ git count-objects\n</code></pre>\n\n<p>Except, <a href="http://code.ohloh.net/file?fid=ixHQFFqenblz1kBNxchs4xCFe6w&amp;cid=S766kQUyJJc&amp;s=%22This%20repository%20currently%20has%20approximately%22&amp;fp=288055&amp;mpundefined=&amp;projSelected=true&amp;filterCheckedundefined=&amp;filterChecked=true&amp;mp=1&amp;ml=1&amp;me=1&amp;md=1#L107" rel="noreferrer">from its source</a>, git-gui will do the math by itself, actually counting something at <code>.git/objects</code> folder and probably brings an approximation (I don\'t know <code>tcl</code> to properly read that!).</p>\n\n<p>In any case, it <strong>seems</strong> to give the warning based on an arbitrary number <em>around</em> 300 loose objects.</p>\n'}, {'answer_id': 22358144, 'author': 'VonC', 'author_id': 6309, 'author_profile': 'https://Stackoverflow.com/users/6309', 'pm_score': 3, 'selected': False, 'text': '<p>You can do it without any interruption, with the new (Git 2.0 Q2 2014) setting <a href="https://git-scm.com/docs/git-config/2.1.0#git-config-gcautodetach" rel="nofollow noreferrer"><strong><code>gc.autodetach</code></strong></a>.</p>\n\n<p>See <a href="https://github.com/git/git/commit/4c4ac4db2c933fbae1c6e6bcf90d9bee5f823fc7" rel="nofollow noreferrer">commit 4c4ac4d</a> and <a href="https://github.com/git/git/commit/9f673f9477c63433aa0774c11b9e42979721f4d6" rel="nofollow noreferrer">commit 9f673f9</a> (<a href="https://github.com/pclouds" rel="nofollow noreferrer">Nguyễn Thái Ngọc Duy, aka pclouds</a>):</p>\n\n<blockquote>\n  <p><code>gc --auto</code> takes time and can block the user temporarily (but not any less annoyingly).<br>\n  Make it run in background on systems that support it.<br>\n  The only thing lost with running in background is printouts. But <code>gc output</code> is not really interesting.<br>\n  You can keep it in foreground by changing <code>gc.autodetach</code>.</p>\n</blockquote>\n\n<hr>\n\n<p>Since that 2.0 release, there was a bug though: git 2.7 (Q4 2015) will make sure to <strong>not lose the error message</strong>.<br>\nSee <a href="https://github.com/git/git/commit/329e6e8794c347d3da92144f88ad838945508ac6" rel="nofollow noreferrer">commit 329e6e8</a> (19 Sep 2015) by <a href="https://github.com/pclouds" rel="nofollow noreferrer">Nguyễn Thái Ngọc Duy (<code>pclouds</code>)</a>.<br>\n<sup>(Merged by <a href="https://github.com/gitster" rel="nofollow noreferrer">Junio C Hamano -- <code>gitster</code> --</a> in <a href="https://github.com/git/git/commit/076c8278583b70547c1c5a65941a661f08210252" rel="nofollow noreferrer">commit 076c827</a>, 15 Oct 2015)</sup>  </p>\n\n<blockquote>\n  <h2><code>gc</code>: save log from daemonized <code>gc --auto</code> and print it next time</h2>\n  \n  <p>While <a href="https://github.com/git/git/commit/9f673f9477c63433aa0774c11b9e42979721f4d6" rel="nofollow noreferrer">commit 9f673f9</a> (<code>gc</code>: config option for running <code>--auto</code> in background - 2014-02-08) helps reduce some complaints about \'<code>gc --auto</code>\' hogging the terminal, it creates another set of problems.</p>\n  \n  <p>The latest in this set is, as the result of daemonizing, <code>stderr</code> is closed and all warnings are lost. This warning at the end of <code>cmd_gc()</code> is particularly important because it tells the user how to avoid "<code>gc --auto</code>" running repeatedly.<br>\n  Because stderr is closed, the user does not know, naturally they complain about \'<code>gc --auto</code>\' wasting CPU.</p>\n  \n  <p><strong>Daemonized <code>gc</code> now saves <code>stderr</code> to <code>$GIT_DIR/gc.log</code>.<br>\n  Following <code>gc --auto</code> will not run and <code>gc.log</code> printed out until the user removes <code>gc.log</code></strong>.</p>\n</blockquote>\n'}, {'answer_id': 23330042, 'author': 'ghiboz', 'author_id': 349045, 'author_profile': 'https://Stackoverflow.com/users/349045', 'pm_score': 2, 'selected': False, 'text': '<p>I use when I do a big commit, above all when I remove more files from the repository.. after, the commits are faster</p>\n'}, {'answer_id': 30247544, 'author': 'Teoman shipahi', 'author_id': 929902, 'author_profile': 'https://Stackoverflow.com/users/929902', 'pm_score': 3, 'selected': False, 'text': '<p>This quote is taken from;\n<a href="https://rads.stackoverflow.com/amzn/click/com/1449316387" rel="noreferrer" rel="nofollow noreferrer">Version Control with Git</a></p>\n<blockquote>\n<p><strong>Git runs garbage collection automatically</strong>:</p>\n<p>• If there are too many loose objects in the repository</p>\n<p>• When a push to a remote repository happens</p>\n<p>• After some commands that might introduce many loose objects</p>\n<p>• When some commands such as  git reflog expire explicitly request it</p>\n<p>And finally, garbage collection occurs when you explicitly request it\nusing the  git gc command. But when should that be? There’s no solid\nanswer to this question, but there is some good advice and best\npractice.</p>\n<p><strong>You should consider running  git gc manually in a few\nsituations:</strong></p>\n<p>• If you have just completed a  git filter-branch . Recall that\nfilter-branch rewrites many commits, introduces new ones, and leaves\nthe old ones on a  ref that should be removed when you are satisfied\nwith the results. All those dead objects (that are no longer\nreferenced since you just removed the one  ref pointing to them)\nshould be removed via garbage collection.</p>\n<p>• After some commands that might introduce many loose objects. This\nmight be a large rebase effort, for example.</p>\n<p><strong>And on the flip side,\nwhen should you be wary of garbage collection?</strong></p>\n<p>• If there are orphaned refs that you might want to recover</p>\n<p>• In the context of  git rerere and you do not need to save the\nresolutions forever</p>\n<p>• In the context of only tags and branches being sufficient to cause\nGit to retain a commit permanently</p>\n<p>• In the context of  FETCH_HEAD retrievals (URL-direct retrievals via\ngit fetch ) because they are immediately subject to garbage collection</p>\n</blockquote>\n'}, {'answer_id': 57290082, 'author': 'Immi', 'author_id': 4239669, 'author_profile': 'https://Stackoverflow.com/users/4239669', 'pm_score': 2, 'selected': False, 'text': '<p>You don\'t have to use <code>git gc</code> very often, because <code>git gc</code> (Garbage collection) is run automatically on several frequently used commands:</p>\n\n<pre><code>git pull\ngit merge\ngit rebase\ngit commit\n</code></pre>\n\n<p>Source: <a href="https://www.atlassian.com/git/tutorials/git-gc" rel="nofollow noreferrer">git gc best practices and FAQS</a></p>\n'}]
['https://Stackoverflow.com/questions/55729', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']