There would be variation in the length of necks in the early giraffe population.
Those who had longer necks would have an advantage for survival as they would be able to reach higher into the trees to feed.
Therefore they would have been more likely to survive and breed.
This would pass on the genes for a longer neck to their offspring.