In an X-ray pulsar, a neutron star accretes matter from a stellar companion through an accretion disk. The high magnetic field of the rotating neutron star disrupts the inner edge of the disc, funneling the gas to flow onto the magnetic poles on its surface. Hercules X-1 is in many ways the prototypical X-ray pulsar; it shows persistent X-ray emission and it resides with its companion HZ Her, a two-solar-mass star, at about 7~kpc from Earth1. Its emission varies on three distinct timescales2 the neutron star rotates every 1.2~seconds, it is eclipsed by its companion each 1.7~days, and the system exhibits a superorbital period of 35~days which has remained remarkably stable since its discovery3. Several lines of evidence point to the source of this variation as the precession of the accretion disc4,5, the precession of the neutron star6,7 or both8. Despite the many hints over the past fifty years, the precession of the neutron star itself has yet not been confirmed or refuted. We here present X-ray polarization measurements with the Imaging X-ray Polarimetry Explorer9 (IXPE) which probe the spin geometry of the neutron star. These observations provide direct evidence that the 35-day-period is set by the free precession of the neutron star crust, which has the important implication that its moment of inertia eigenvalues differ fractionally by a few parts per ten million10. Furthermore, we find indications that the basic spin geometry of the neutron star is altered by torques on timescale of a few hundred days.