Grid cells are neurons with periodic spatial receptive fields (grids) that tile two-dimensional space in a hexagonal pattern. To provide useful information about location, grids must be stably anchored to an external reference frame. The mechanisms underlying this anchoring process have remained elusive. Here we show in differently sized familiar square enclosures that the axes of the grids are offset from the walls by an angle that minimizes symmetry with the borders of the environment. This rotational offset is invariably accompanied by an elliptic distortion of the grid pattern. Reversing the ellipticity analytically by a shearing transformation removes the angular offset. This, together with the near-absence of rotation in novel environments, suggests that the rotation emerges through non-coaxial strain as a function of experience. The systematic relationship between rotation and distortion of the grid pattern points to shear forces arising from anchoring to specific geometric reference points as key elements of the mechanism for alignment of grid patterns to the external world.