Delineating ecologically meaningful populations among microbes is important for identifying their roles in environmental and host-associated microbiomes. Here, we introduce a metric of recent gene flow, which when applied to co-existing microbes, identifies congruent genetic and ecological units separated by strong gene flow discontinuities from their next of kin. We then develop a pipeline to identify genome regions within these units that show differential adaptation and allow mapping of populations onto environmental variables or host associations. Using this reverse ecology approach, we show that the human commensal bacterium Ruminococcus gnavus breaks up into sharply delineated populations that show different associations with health and disease. Defining populations by recent gene flow in this way will facilitate the analysis of bacterial and archaeal genomes using ecological and evolutionary theory developed for plants and animals, thus allowing for testing unifying principles across all biology.