How and at what pace bacteria evolve when colonizing healthy hosts remains unclear. Here, by monitoring evolution for more than six thousand generations in the mouse gut, we show that the successful colonization of an invader Escherichia coli depends on the diversity of the existing microbiota and the presence of a closely related strain. Following colonization, two modes of evolution were observed: one in which diversifying selection leads to long-term coexistence of ecotypes and a second in which directional selection propels selective sweeps. These modes can be quantitatively distinguished by the statistics of mutation trajectories. In our experiments, diversifying selection was marked by the emergence of metabolic mutations, and directional selection by acquisition of prophages, which bring their own benefits and costs. In both modes, we observed parallel evolution, with mutation accumulation rates comparable to those typically observed in vitro on similar time scales. Our results show that gut environments can rapidly generate diversifying selection and ecotype formation.