The nature of the jets and the role of magnetic fields in gamma-ray bursts (GRB) remains unclear. In a baryon-dominated jet only weak, tangled fields generated in situ through shocks would be present. In an alternative model, jets are threaded with large scale magnetic fields that originate at the central engine and which accelerate and collimate the jets. The way to distinguish between the models is to measure the degree of polarization in early-time emission, however previous claims of gamma-ray polarization have been controversial. Here we report that the early optical emission from GRB 090102 was polarized at the level of P=10+/-1%, indicating the presence of large-scale fields originating in the expanding fireball. If the degree of polarization and its position angle were variable on timescales shorter than our 60-s exposure, then the peak polarization may have been larger than 10 per cent.