Belief in a spirit guardian of the city moat and walls, the literal meaning of chenghuang, predates the Ming dynasty by centuries, with scattered local cults attested from at least the Southern and Northern Dynasties period, but the temple type reached its familiar, empire-wide standardized form only after the Ming founder, the Hongwu Emperor, ordered in the 1370s that every prefecture, subprefecture and county maintain an officially ranked City God temple, its deity assigned a bureaucratic rank mirroring the human magistrate serving the same jurisdiction.
The reform was explicit in its logic: Hongwu is recorded as believing that a magistrate who knew an unseen divine counterpart was watching his conduct, ready to report wrongdoing in the spirit bureaucracy just as human officials reported to their own superiors, would govern more honestly, effectively fusing local religious cult and state administrative control into a single system of parallel, mutually reinforcing surveillance. New magistrates arriving to take up a post were expected to formally visit the local City God temple and report their arrival to the god before assuming secular office, and the temple's standing within a jurisdiction's prestige hierarchy generally tracked the rank and importance of the human government seat it stood beside, larger prefectural capitals warranting grander temples and higher-ranked gods than modest county towns.