G5 is the fruit of Luffy 17+ years of training
Devil Fruit awakening is achieved through hard work, rigorous training, and pushing your abilities to their limits.
And that's what Luffy did.
After getting defeated by akoji and cp9
Luffy comes up with gear 2 and gear 3 something he had been thinking about since he lost to Aokiji.
It's not some magic either. He's doing something he trained 10 whole years for. Also, something that comes with a huge price.
That's when his real journey start with him creating a whole new fighting style from scratch. He trains for 10 years in extreme conditions to develop this.
He learns to make use of this nerf in best way possible while incorporating different martial arts into his own style.