Bacteria of the Firmicutes phylum are able to enter a developmental pathway that culminates with the formation of highly resistant, dormant endospores. Endospores allow environmental persistence, dissemination and for pathogens, are also infection vehicles. In both the model Bacillus subtilis, an aerobic organism, and in the intestinal pathogen Clostridioides difficile, an obligate anaerobe, sporulation mobilizes hundreds of genes. Their expression is coordinated between the forespore and the mother cell, the two cells that participate in the process, and is kept in close register with the course of morphogenesis. The evolutionary mechanisms by which sporulation emerged and evolved in these two species, and more broadly across Firmicutes, remain largely unknown. Here, we trace the origin and evolution of sporulation using the genes known to be involved in the process in B. subtilis and C. difficile, and estimating their gain-loss dynamics in a comprehensive bacterial macroevolutionary framework. We show that sporulation evolution was driven by two major gene gain events, the first at the base of the Firmicutes and the second at the base of the B. subtilis group and within the Peptostreptococcaceae family, which includes C. difficile. We also show that early and late sporulation regulons have been coevolving and that sporulation genes entail greater innovation in B. subtilis with many Bacilli lineage-restricted genes. In contrast, C. difficile more often recruits new sporulation genes by horizontal gene transfer, which reflects both its highly mobile genome, the complexity of the gut microbiota, and an adjustment of sporulation to the gut ecosystem.