Kirk could have met Gary in 2250 like he said in the second pilot but didn't teach him until years later after he was a lieutenant and came back to Earth to be a student instructor. Perhaps after the crew of the Farragut was wiped out by the dikironium cloud creature in 2257?