  We show that the continuum limit of moir\'e graphene is described by a
$(2+1)$-dimensional field theory of Dirac fermions coupled to two classical
vector fields: a periodic gauge and spin field. We further show that the
existence of a flat band implies an effective dimensional reduction, where the
time dimension is ``removed.'' The resulting two-dimensional Euclidean theory
contains the chiral anomaly. The associated Atiyah-Singer index theorem
provides a self-consistency condition for flat bands. In the Abelian limit,
where the spin field is disregarded, we reproduce a periodic series of
quantized magic angles known to exist in twisted bilayer graphene in the chiral
limit. However, the results are not exact. If the Abelian field has zero total
flux, perfectly flat bands can not exist, because of the leakage of edge states
into neighboring triangular patches with opposite field orientations. We
demonstrate that the non-Abelian spin component can correct this and completely
flatten the bands via an effective renormalization of the Abelian component
into a configuration with a non-zero total flux. We present the Abelianization
of the theory where the Abelianized flat band can be mapped to that of the
lowest Landau level. We show that the Abelianization corrects the values of the
magic angles consistent with numerical results. We also use this criterion to
prove that an external magnetic field splits the series into pairs of magnetic
field-dependent magic angles associated with flat moir\'e-Landau bands. The
topological criterion and the Abelianization procedure provide a generic
practical method for finding flat bands in a variety of material systems
including but not limited to moir\'e bilayers.
