Dysfunction in the prefrontal cortex can lead to cognitive inflexibility due to multifactorial causes as included cardiometabolic disorders, stress, inadequate diets, as well as an imbalance of the gut–brain axis microbiota. However, these risk factors have not been evaluated jointly. The purpose of this study was to evaluate the effect of physical stress (MS: Male Stress and FS: Female Stress) and high-fat diet (MD: Male Diet and FD: Female Diet) supplementation on the gut microbiota and cognitive flexibility. Methods: The study was performed on 47 mice, 30 male (M) and 17 female (F) BALBc, exposed to chronic stress physical (S) and high-fat diet (D). Cognitive flexibility was evaluated using the Attentional Set-Shifting Test (ASST) and the gut microbiota composition in terms of relative abundance (%) and alpha–beta diversity. Results: Results showed that S and D reduced cognitive flexibility in male and female mice (p < 0.0001). Significant changes occurred in Alistipes spp. (MM vs. MS:MD; p < 0.0001), Barnesiella spp. (FC vs. FS; p = 0.0002; FC vs. FD, p = 0.0033); Dorea spp. (MC vs. MD, p = 0.0008; MM vs. MD, p < 0.0001) and Lactobacillus spp. (MC vs. MD and FM vs. FS, p < 0.0001; FM vs. MD, p = 0.0393) genera among groups. Predictive functional analysis (QIIME2 and PICRUSt2) showed a significant increase in the expression of histidine kinase, alanine dehydrogenase, glutamine synthase, glutamate synthase, arginine succinyl synthase, and tryptophan synthase genes (p < 0.05), the latter being a precursor of serotonin (5-HT). Conclusions: Chronic physical stress and a high-fat diet modify cognitive flexibility and the composition and predictive function of the gut microbiota.