Habitat matching - the ability for an individual to choose its habitat according to the match between the local environment and its own phenotype - may favor population adaptation and range shift under climate change. The ability of individuals to choose their habitat may however be restricted under contemporary global change as landscape fragmentation also currently increases. Understanding the consequences of habitat matching on the responses of populations to global change is therefore crucial to predict future species distribution. Here we modeled the effect of habitat matching on range shift dynamics under climate change. We compared random dispersal (i.e. constant emigration probability, no habitat choice) with habitat matching (i.e. emigration probability and habitat choice dependent of the match between environment and individual's phenotype). We found that population extinction could be up to twice higher under random dispersal than under habitat matching. Habitat matching reduces the risk of extinction of core populations, which limits the fragmentation of the range. It also reduces the risk of extinction at the margins of the populations, which limits range contraction. We show that habitat matching reduces the maladapted gene flow that leads to extinction by promoting both the emigration of maladapted individuals and the settlement of adapted individuals. Our results highlight the importance of considering habitat matching to better predict the responses of populations under global change.