The evolution of genetic diversity in changing environments