Climate change is increasing extreme heating events and the potential for disease outbreaks. Whether hosts can adapt to infection with rising temperatures is important for forecasting species persistence. We tested whether warming-at different host life stages-affects the ecological and evolutionary dynamics of resistance in Caenorhabditis elegans infected by a wild bacterial pathogen. We competed resistant and susceptible genotypes across 10 passages and tracked the spread of resistance in the population. Infection and prolonged warming strongly selected for the resistant genotype. Warming during host development induced plastic defences against infection, reducing the selective pressure for costly genetic-based resistance. Resistance was lost under ambient temperatures and periodic warming. Selection for resistance was likely weakened at ambient temperatures by the dilution effect, whereby the resistant genotype reduced pathogen transmission. Evolutionary dynamics of resistance depend on the balance among pathogen virulence, costs of genetic-based resistance, the dilution effect and plastic defences induced by temperature stress.