In this report, we demonstrate on the preparation of mesoporous graphitic carbon nitride (gCN) with 3D porous structure using dicyandiamide as a CN precursor and KIT-6 as a template via hard-template replication method and its superior photocatalytic hydrogen evolution activity under simulated sunlight. The powder X-ray diffraction, nitrogen adsorption and near edge X-ray absorption fine structure (NEXAFS) results reveal that the prepared materials possess gCN crystal structure with ordered mesopores and a 3D porous structure and a high specific surface area (221 m2/g). UV–Vis and PL spectra show that the absorption edges of mesoporous gCNs are slightly blue-shifted and the PL intensity is remarkably suppressed. The photocatalytic hydrogen evolution activity of gCN-KIT-6 under solar simulated irradiation is 1.92 mmol·g−1 h−1 which is much higher than that of gCN with 2D mesoporous structure, gCN from silica nanoparticle and bulk nonporous gCN. This high photocatalytic activity of gCN-KIT-6 can be attributed to 3D structure and the retarded electron-hole recombination.