Defect formation and local gauge invariance