title: When exactly does git prune objects: why is "git gc" not removing commits?
body: <p>I'm working on a git course and wanted to mention that lost refs are not really lost until running <code>git gc</code>. But verifying this, I found out that this is not the case. Even after running <code>git gc --prune=all --aggressive</code> the lost refs are still there.</p>
  
  <p>Clearly I misunderstood something. And before saying something incorrect in the course, I want to get my facts straight! Here is an example script illustrates the effect:</p>
  
  <pre><code> #!/bin/bash
  
   git init
  
   # add 10 dummy commits
   for i in {1..10}; do
       date &gt; foo.txt
       git add foo.txt
       git commit -m "bump" foo.txt
       sleep 1
   done;
  
   CURRENT=$(git rev-parse HEAD)
   echo HEAD before reset: ${CURRENT}
  
   # rewind
   git reset --hard HEAD~5
  
   # add another 10 commits
   for i in {1..10}; do
       date &gt; foo.txt
       git add foo.txt
       git commit -m "bump" foo.txt
       sleep 1
   done;
  </code></pre>
  
  <p>This script will add 10 dummy commits, reset to 5 commits in the past and add another 10 commits. Just before resetting, it will print the hash of it's current HEAD.</p>
  
  <p>I would <em>expect</em> to lose the object in <code>CURRENT</code> after running <code>git gc --prune=all</code>. Yet, I can still run <code>git show</code> on that hash.</p>
  
  <p>I do understand that after running <code>git reset</code> and adding new commits, I have essentially created a new branch. But my original branch no longer has any reference, so it does not show up in <code>git log --all</code>. It also would not be pushed to any remote I suppose.</p>
  
  <p>My understanding of <code>git gc</code> was that is removes those objects. This does not seem to be the case.</p>
  
  <p>Why? And <em>when</em> exactly does <code>git gc</code> remove objects?</p>