The photocatalytic activity of g-C3N4 can be enhanced through precise defect engineering. In this study, hierarchical porous g–C3N4–containing cyano groups and N vacancies was synthesized by thermally polymerizing dicyandiamide precursor in a hydrochloric acid solution of magnesium chloride. The resulting structure comprised crosslinked rod-shaped units with a high aspect ratio, facilitating efficient light transmission and absorption within the material. Furthermore, introducing N defects reduced the bandgap and optimized the electronic structure, thereby enhancing light absorption and exposing active sites effectively. The synergistic effects of N defects and the hierarchical porous structure disrupted the original electronic distribution of g-C3N4, leading to directional separation of photogenerated charge carriers and considerably enhancing hydrogen evolution performance. Consequently, the optimized hpCNX-0.05 sample exhibited markedly improved hydrogen evolution rates, achieving 1703.67 μmol g−1 h−1 under visible light irradiation, approximately 3.16 times higher than that of bulk g-C3N4. Overall, this paper presents a promising strategy for synthesizing hierarchical porous g-C3N4 with N defects to achieve highly efficient H2 production.