Generalized-stacking-fault energy (GSFE) serves as an important metric that prescribes dislocation behaviors in materials. In this paper, utilizing first-principle calculations and chemical bonding analysis, we studied the behaviors of generalized stacking fault in graphene and h-BN. It has been shown that the π bond formation plays a critical role in the existence of metastable stacking fault (MSF) in graphene and h-BN lattice along certain slip directions. Chemical functionalization was then proposed as an effective means to engineer the π bond, and subsequently MSF along dislocation slips within graphene and h-BN. Taking hydrogenation as a representative functionalization method, we demonstrated that, with the preferential adsorption of hydrogen along the slip line, π electrons along the slip would be saturated by adsorbed hydrogen atoms, leading to the moderation or elimination of MSF. Our study elucidates the atomic mechanism of MSF formation in graphene-like materials, and more generally, provides important insights towards predictive tuning of mechanic properties in two-dimensional nanomaterials.