The control of the GaN-polarity is promising in view of achieving periodic polarity structures for non-linear optics. It is shown here that the polarity of GaN(0 0 0 1) epilayers can be reversed from Ga to N using a high Mg doping during molecular beam epitaxy with NH3 as nitrogen precursor. Transmission electron microscopy (TEM) studies indicate that the N-polar crystal quality is not degraded compared to the initial Ga-polar GaN. The structure, thickness and distance from the doping start of the inversion domain boundaries are studied by TEM. The polarity inversion dependence upon the Mg doping level is explored. The results show that a critical Mg surface coverage is necessary for polarity inversion and that Mg surface segregation plays a key role. Finally, a transition from hexagonal N-polar GaN to cubic GaN is demonstrated at high doping level. (C) 2004 Elsevier B.V. All rights reserved.