The effect of hydrogen on Mg incorporation for both polar and nonpolar GaN surfaces is explored using density functional total energy calculations. A thermodynamic approach is employed, with chemical potentials appropriate for realistic growth conditions. It is shown that hydrogen stabilizes new Mg-rich surface reconstructions for both the (0001) and $(10\underset{̱}{1}0)$ surfaces. Hydrogen greatly enhances the stability of Mg-rich reconstructions of the $m$ plane. Experimental results for $p$-type doping obtained in growth on both the $m$-plane and $c$-plane surfaces can be understood on the basis of these results. A laterally contracted row model for the $\mathrm{Ga}\mathrm{N}(10\underset{̱}{1}0)$ surface is shown to be energetically favorable in Ga-rich conditions.