Capturing glucose dynamics including the rigorous fasting glucose homeostasis and postprandial glucose adaptation is central to the diagnosis, subtyping, early warning, lifestyle intervention, and treatment for type 2 diabetes (T2D). Recently, continuous glucose monitoring (CGM) technology has revolutionized fields to track real-time blood glucose levels and trends, and facilitated safe and effective decision making for diabetes management. Here, we developed an attention-based deep learning model, CGMformer, pretrained on a large-scale and diverse corpus of CGM data from a nationwide multi-center study in China to enable context-specific predictions and clinical applications to individuals. During pretraining, CGMformer gained a fundamental understanding of glucose dynamics, encoded glucose value, fluctuation pattern, hyperglycemia, and hypoglycemia in the attention weights of the model in a completely self-supervised manner. Fine-tuning towards a diverse panel of downstream tasks relevant to the diagnosis and treatment of diabetes and complications using task-specific data demonstrated that CGMformer consistently boosted predictive accuracy. By deciphering individual glucose dynamics, CGMformer allows us to subtype individuals with high T2D risk and identify a specific cluster of lean prediabetes that is easily overlooked by traditional glucose measurements. In particular, applied to dietary modification modelling, CGMformer predicted individual's postprandial glucose response or CGM curve, thereby provided personalized diet prescription suggestion. Overall, CGMformer represents a pretrained transformer model to decode individual glucose dynamics, from which fine-tuning towards a broad range of downstream applications can be pursued to promote T2D early warning and recommendation for therapeutic lifestyle modification in diabetes management.